日期:2014-05-16 浏览次数:21097 次
转自:http://ilexes.blog.51cto.com/705330/531352
注:此ssh非彼SSH(Struts+Spring+Hibernate)?
在Java中,我们可以通过Runtime去执行一些OS的命令,如:?
String[]
shell = new String[] { " /bin/sh " , " -c " , " ls -l " } ;?
Process p =
Runtime.getRuntime().exec(shell);?
通过在Linux上执行 ssh --help命令,?
usage: ssh
[-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]?
[-D
[bind_address:]port] [-e escape_char] [-F configfile]?
[-i identity_file] [-L
[bind_address:]port:host:hostport]?
[-l login_name] [-m mac_spec] [-O
ctl_cmd] [-o option] [-p port]?
[-R [bind_address:]port:host:hostport] [-S
ctl_path]?
[-w local_tun[:remote_tun]] [user@]hostname
[command]?
不难发现,ssh命令中并不能带密码。?
如果不需要登录时,我们可以用这样的方式来执行:?
String[] shell = new String[] { " /bin/sh " , " -c " , " ssh root@192.168.1.5 ls -l " } ;
Process p = Runtime.getRuntime().exec(shell);
??public static String sshExecute(String host, String user, String pwd,
String command) {
String osName = System.getProperty("os.name");
// ps -ef|grep tomcat|grep -v grep|awk '{print $2}'
StringBuffer sb = new StringBuffer();
try {
JSch jsch = new JSch();
if (osName.toUpperCase().indexOf("WINDOWS") > -1) {
jsch.setKnownHosts("c:\\known_hosts");
} else {
jsch.setKnownHosts("/root/.ssh/known_hosts");
}
Session session = jsch.getSession(user, host, 22);
session.setPassword(pwd);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
InputStream in = channel.getInputStream();
channel.connect();
int nextChar;
while (true) {
while ((nextChar = in.read()) != -1) {
sb.append((char) nextChar);
}
if (channel.isClosed()) {
System.out.println("exit-status: "
+ channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
session.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
?CZtvq8ojTYt7NcAjjtW2FqmFNO+5x/mTwyY+ssoP5SganxDYs3G016aPZDQdGVZMn/08Is
B7QEIAXEVHtgGIGuLqsdMUBBIxV7KI6BK+OWVwv277tBOgqvPcgeEacviFZb2lZhWr8hv
R2pTrPFBLr+UELejm/Nnf9qWDBjDj/d2o8+ReSwN8dzNJIiFyWdboyUCZfHhxNgiGANFx22
gC4lM+Wk8gkASo/QYDvoUwLFrAJoMdsw0d4pn34bQ0mspaaWy4N0+zrNCPcl8D0Q1R
rAjfYfOiZOSTnxabJ2DLijuq7UgFbn2ESMw==?
附:产生此文件的简单方法:可以在Linux中用ssh命令登录一次,这样便在/root/.ssh(假如使用root用户登录到Linux)目录下生成known_hosts文件。