java远程调用linux shell命令或shell脚本
                
                
                
                    
                        - 摘要:工具、jar包:ganymed-ssh2.jar编程方式:Connectionconn=newConnection(ip);conn.connect();conn.authenticateWithPassword(usrname,password);Sessionsession=conn.openSession();session.executeCommand("xxxxx.sh");//到这里就执行完毕了,但是有时候老是碰到执行失败的情况,所以要执行下面的步骤以得到执行该命令的返回信息
- 标签:Linux Java 命令 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());