Ganymed SSH-2 for Java

Ganymed SSH-2 用标准java (tested on J2SE 1.4.2 and 5.0) 实现了SSH-2 protocol.

 

It allows one to connect to SSH servers from within Java programs. It supports SSH sessions (remote command execution and shell access), local and remote port forwarding, local stream forwarding, X11 forwarding, SCP and SFTP. There are no dependencies on any JCE provider, as all crypto functionality is included.

 

Connection conn = new Connection(ServerHost);
boolean isAuthenticated = conn.authenticateWithPassword(toServerUserName, toServerPassword); //支持密码链接和私钥链接(ssh建立无密码链接)

Session sess = conn.openSession();
sess.execCommand(command);

sess.getStdout();//返回执行命令后信息
sess.getStderr();//返回错误信息

 

注意的地方:如果你需要执行多个 linux 控制台脚本,比如第一个脚本的返回结果是第二个脚本的入参,你必须打开多个 Session, 也就是多次调用

Session sess = conn.openSession(); , 使用完毕记得关闭就可以了

你可能感兴趣的:(java,linux,J2SE,ssh,脚本)