java远程连接linux并启动脚本
java启动远程脚本
说真的这样的方式,是真的不太好,因为远程连接需要时间,你用xshell这个玩意连接需要点时间,那么我给你的代码连接也需要时间,我觉得采用我上篇文章那个好点,但是有的时候又不得不用这样的方式,说远了,代码搞起来
public static String executeShell(DestHost host, String[] commands) {
Session session = getSession(host);
PrintWriter out = new PrintWriter(session.getStdin());
for (String command : commands) {
out.println(command);
}
out.flush();
out.close();
try {
session.waitForCondition(ChannelCondition.CLOSED | ChannelCondition.EOF |
ChannelCondition.EXIT_STATUS , TIME_OUT);
} catch (Exception e) {
e.printStackTrace();
log.error("连接服务执行脚本失败;",e)