返回信息流做了一个小框架,也可以叫做小toolkit吧。把它开源了,放在了Google Code上http://code.google.com/p/sshxcute/,有兴趣的童鞋可以看一看。
解决的问题是这样的,你可以利用Java代码通过SSH连接远程执行Linux/UNIX系统上的命令或者脚本,对自动化测试或者喜欢Java的系统admin做环境部署还是有一定用处的。说起来比较拗口,举个例子吧,假如你想在远程Linux上执行几个命令"ls -al" > "pwd" > "/opt/productA/bin/abcd.sh -user admin -password 12345",通过sshxcute就可以在本地的Java IDE(比如Eclipse)中做,如果你说完全可以写个shell扔到server上,那当然可以,这里只是一种选择吧。
SSHExec ssh = null;
try {
ConnBean cb = new ConnBean("ip", "username","password");
ssh = SSHExec.getInstance(cb);
CustomTask ct1 = new ExecCommand("ls -al","pwd");
CustomTask ct2 = new ExecShellScript("/opt/productA/bin/","./abcd.sh ","-user admin -password 12345");
ssh.connect();
ssh.exec(ct1);
Result res = ssh.exec(ct2);
if (res.isSuccess)
{
System.out.println("Return code: " + res.rc);
System.out.println("sysout: " + res.sysout);
}
else
{
System.out.println("Return code: " + res.rc);
System.out.println("error message: " + res.error_msg);
}
}
catch (TaskExecFailException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
finally
{
ssh.disconnect();
}
sshxcute是基于jsch写的,只是做了一层包装让它用起来更加友好,有熟悉STAF/STAX的童鞋会有体会,sshxcute比较接近它的API用法。
不知道写些什么了,广告打的太嚣张了,做了些东西闷在家里不如让大家鞭笞下,尽情的拍我吧~ 哈哈哈
这是一条镜像帖。来源:北邮人论坛 / java / #15671同步于 2010/9/11
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
(开源分享)sshxcute框架/小工具之广告篇...轻拍
neo861002
2010/9/11镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复