【发布时间】:2017-03-19 09:48:40
【问题描述】:
我正在使用JSch在多级ssh之后运行一些命令:
public static void main(String[] args) {
String user="User0";
String ip="IP0";
int port=22;
String password="Password0";
JSch jsch= new JSch();
Session session=null;
ChannelExec channel=null;
try {
session=(jsch.getSession(user, ip, port));
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
String dir=Reomte_DIR;
String cmd1=SomeComplexCommand;
String cmd2=SomeMoreComplexCommand;
channel = (ChannelExec) session.openChannel("exec");
channel.setInputStream(null);
channel.setCommand("ssh User1@IP1_PasswordLessLogin;ssh User2@IP2_PasswordLessLogin; "+cmd1+" ; "+cmd2+" ;");
channel.setPty(true);
channel.connect(4000);
String res = null;
BufferedReader input = new BufferedReader(new InputStreamReader(channel.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(channel.getErrStream()));
if ((res = error.readLine()) == null) {
res = input.readLine()+input.readLine()+input.readLine()+input.readLine();
} else {
res = "-1";
}
System.out.println("result:"+res);
} catch (JSchException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
channel.disconnect();
session.disconnect();
}
}
但它没有给出预期的结果。
事实上channel.getInputStream() 挂起。如果我删除多级 ssh,一切正常!
难道我做错了什么??
我得到了一些提示:Multi-level SSH login in Java 和 Multiple commands using JSch,但我无法让我的代码运行。
【问题讨论】:
-
你的命令错了,你是不是先在命令行上试过?