读书人

Ganymed SSH-二 for Java

发布时间: 2013-11-08 17:52:32 作者: rapoo

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 withinJava 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();,使用完毕记得关闭就可以了

读书人网 >编程

热点推荐