`
zhangxiong0301
  • 浏览: 351800 次
社区版块
存档分类
最新评论

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

    博客分类:
  • JAVA
阅读更多
工具、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());
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics