java远程调用linux shell命令或shell脚本

工具、jar包:ganymed-ssh2.jar
编程方式:
   Connection conn = new Connection(ip);
   conn.connect();
   conn.authenticateWithPassword(usrname,password);
   Session session = conn.openSession();
   session.executeCommand("xxxxx.sh");

//到这里就执行完毕了,但是有时候老是碰到执行失败的情况,所以要执行下面的步骤以得到执行该命令的返回信息,包括出错信息,从而做出修改
   int count=0;
   ByteArrayOutputStream data = new ByteArrayOutpuStream();
   byte[] output = new byte[1024];
   InputStream input = new StreamGobbler(session.getStdout());
   while( count = input.read(output) != -1 )
   {
      data.write(output);
   }
    System.out.println(data.toString());

你可能感兴趣的:(java,编程,linux,脚本)