http://www.ganymed.ethz.ch/ssh2/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class SSH2Demo {
public static void main(String[] args)
{
String hostname = "***";
String username = "***";
String password = "***";
try
{
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* Now connect */
conn.connect();
/* Authenticate.
* If you get an IOException saying something like
* "Authentication method password not supported by the server at this stage."
* then please check the FAQ.
*/
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
/* Create a session */
Session sess = conn.openSession();
/* execute Linux command */
sess.execCommand("ls");
System.out.println("Here is some information about the remote host:");
/* get result InputStream */
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
/* ExitCode=0 is success, ExitCode!=0 is fail */
System.out.println("ExitCode: " + sess.getExitStatus());
/* Close this session */
sess.close();
/* Close the connection */
conn.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(2);
}
}
}
分享到:
相关推荐
java通过代码登录远程linux服务器并执行linux命令源码以及jar包
JAVA通过SMB的方式下载、上传远程Linux服务器的文件
java上传文件到linux服务器,操作linux服务器上文件,下载linux服务器文件,删除linux服务器文件。ganymed-ssh2远程操作服务器文件
主要实现的是登录服务器操作服务器的中的文件数据,支持读写的操作。主要使用的方法getProperties是设置配置的login(参数一是访问服务器的配置,参数二是设置读还是写)方法是读写连接服务器
主要介绍了Java远程连接Linux服务器并执行命令及上传文件功能,本文是小编整理的代码笔记,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
Linux服务器批处理远程命令执行工具。纯java实现的ssh协议(jsch),支持同时连接多台linux主机,执行相同的命令。只需配置目标主机的ip地址、用户名和密码即可实现远程操作。服务器无需配置ssh对等协议或安装任何...
本地cmd命令, 输入命令, 并在控制台输出命令执行结果 jsch 远程ssh到linux服务器, 执行结果并显示输出
获取linux服务器内存,cpu,硬盘等信息java代码,sigar实现!
主要介绍了Java中通过jsch来连接远程服务器执行linux命令的相关资料,需要的朋友可以参考下
java远程访问linux服务器操作 远程执行shll脚本或者命令、上传下载文件 package com.szkingdom.kfit.bank.ccbDirectShortcut.helper; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.SCPClient; import ...
java 读取 各平台 服务器硬件信息(windowx+linux+Mac OS) 案例............。
通过运行Java程序可以控制远程Linux服务器,可以向远程服务器发送指令,也可以接受服务器的信息,如果有兴趣,可以和我一起讨论。
JAVA代码实现远程服务器的文件操作步骤及JAR包.zip
远程上传及同步文件夹工具,从MS-Win7客户端上传文件夹到远程Linux服务器,如文件夹的部分内容已存在远程服务器,则可根据选项进行同步更新。可用于部署和同步本地开发的服务器代码到远程服务器。 完全用Java开发的...
通过win7远程连接linux服务器上的oracle
Java运用ganymed-ssh2-build210.jar包远程连接操作linux服务器 (SCP和SFTP的运用)
java判断远程服务器上的文件是否存在的方法,需要的朋友可以参考一下
ssh工具 用于连接远程服务器 linux 系统 解压 安装即可使用
使用Eclipse远程调试Linux服务器Tomcat项目的一些简单的调试技巧,