最近在用ssh2远程Shell脚本执行工具 写了一个 代码 测试 执行普通的命令都可以 比如 “ls” 啦什么的都可以
但是执行动态的 就不可以 比如“top” 也没有错误 只是返回的是“”。求大家帮忙呀 !
下面是代码 main是测试 用的是 root的权限
package com.syxp.sjyw.util;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;import com.syxp.sjyw.model.ScheduledTask;/**
* 远程Shell脚本执行工具

* @author Administrator
*/
public class RemoteShellTool implements SSHHelper {private Connection conn;
private String ipAddr;
private String charset = Charset.defaultCharset().toString();
private String userName;
private String password;
private String cmd;public RemoteShellTool() {}public RemoteShellTool(String ipAddr, String userName, String password, String charset) {
   this.ipAddr = ipAddr;
   this.userName = userName;
   this.password = password;
   if(charset != null) {
    this.charset = charset;
   }
}/**
* 登录远程Linux主机

* @return
* @throws IOException
*/
public boolean login() throws IOException {
   conn = new Connection(ipAddr);
   conn.connect(); // 连接
   return conn.authenticateWithPassword(userName, password); // 认证
}
/**
* 执行Shell脚本或命令

* @param cmds 命令行序列
* @return
*/
public String exec(String cmds) {
   InputStream in = null;
   String result = "";
   try {
    if (this.login()) {
     Session session = conn.openSession(); // 打开一个会话
     session.execCommand(cmds);
     in = session.getStdout();
     result = this.processStdout(in, this.charset);
     conn.close();
    }
   } catch (IOException e1) {
    e1.printStackTrace();
   }
   return result;
}/**
* 解析流获取字符串信息

* @param in 输入流对象
* @param charset 字符集
* @return
*/
public String processStdout(InputStream in, String charset) {
   byte[] buf = new byte[1024];
   StringBuffer sb = new StringBuffer();
   try {
    while (in.read(buf) != -1) {
     sb.append(new String(buf, charset));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   return sb.toString();
}public static void main(String[] args) {

RemoteShellTool est = new RemoteShellTool("192.168.248.128", "root", "woaiwojia", "utf-8");
try {
if(est.login()) {
System.out.println(est.exec("top"));
//System.out.println(est.exec("exit"));
} else {
System.out.println("*** 连接失败 ***");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}public String execCmd(String ipAddr, String username, String password, String charset, String cmd) {
this.ipAddr = ipAddr;
this.userName = username;
this.password = password;
this.charset = charset;    if(charset != null) {
    this.charset = charset;
   }

try {
if(login()) {
String s = exec(cmd);
exec("exit");
return s;
} else {
//System.out.println("连接失败");
return "*** 连接失败 ***";
}
} catch (IOException e) {
e.printStackTrace();
return "*** SSH 异常 ***";

}public String execCmd(ScheduledTask st) {

   this.ipAddr = st.getIp();
   this.userName = st.getUsername();
   this.password = st.getPassword();
   if(charset != null) {
    this.charset = st.getCharset();
   }

try {
if(login()) {
String s = exec(st.getCmd());
exec("exit");
return s;
} else {
//System.out.println("连接失败");
return "*** 连接失败 ***";
}
} catch (IOException e) {
e.printStackTrace();
return "*** SSH 异常 ***";



}public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCmd() {
return cmd;
}
public void setCmd(String cmd) {
this.cmd = cmd;
}}