我已经解决了这个问题: 我的解决方案 我将ErrStream打印成'in2',然后将其打印出来,就像'in'
一样InputStream in2 = ((ChannelExec) channel).getErrStream();
我的目标:
- 在textview上从System.err打印错误消息“bash:dumper:command not found”(如android监视器所示)
我的问题:
- 如何从System.err读取错误消息?
代码是什么:
-get来自用户的ssh命令然后显示为返回
public void ssh_send_cmd(String input_cmd) {
ssh_input_cmd = input_cmd;
Runnable runnable = new Runnable() {
public void run() {
IDList.clear();
Connection connection = null;
Session session= null;
try {
JSch jsch = new JSch();
// Get SSH session
session = jsch.getSession(Global.servUser, Global.host, Global.port);
session.setPassword(Global.servPwd);
java.util.Properties config = new java.util.Properties();
// Never automatically add new host keys to the host file
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
// Connect to remote server
session.connect();
System.out.println("Connection through ssh established!");
String command = ssh_input_cmd;
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
System.out.println("Connect to session...");
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
update_monitor(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
update_monitor("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
System.out.println(ee);
}
}
channel.disconnect();
}catch (Exception e) {
System.out.println(e);
update_monitor("error 1");
}finally{
if(session !=null && session.isConnected()){
session.disconnect();
}
}
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
Android监视器:
com.example.acer.ssh_cmd I / System.out:通过ssh建立连接!
com.example.acer.ssh_cmd I / System.out:连接到会话......
com.example.acer.ssh_cmd W / System.err: bash:dumper:command not found
com.example.asus.ssh_cmd I / System.out:exit-status:127
bash:dumper:找不到命令
供您参考:“dumper”是用户输入的,未找到命令