JSCH 的怪异行为
Weird Behaviour of JSCH
我正在尝试使用 JSCH 通过 SSH 执行多个命令 library.I 我遇到了一个奇怪的问题 issue.When 我尝试 运行 eclipse 中的程序代码完成了执行但是命令(mkdir 测试)未在 server.But 中执行,当我逐步调试命令被执行并且文件夹被 created.Can 任何人解释我在这里做错了什么?代码如下:
public static void runCommand() throws JSchException, IOException{
JSch jsch=new JSch();
Session session=jsch.getSession("user","linuxip",22);
List<String>commandList=new ArrayList<String>();
commandList.add("cd deploy");
commandList.add("mkdir test");
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
session.setTimeout(500);
System.out.println("Connecting..");
Channel channel = session.openChannel("exec");
OutputStream out = channel.getOutputStream();
((ChannelExec) channel).setCommand("sudo su - wsp ");
System.out.println("connected..");
// channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
// InputStream in = channel.getInputStream();
((ChannelExec) channel).setPty(true);
channel.connect();
channel.setOutputStream(System.out);
for(String command:commandList) {
out.write((command+"\n").getBytes());
}
out.write(("exit"+"\n").getBytes());
out.flush();
out.close();
channel.disconnect();
session.disconnect();
}}
最终发现 solution.I 犯了一个愚蠢的错误,将会话超时设置为 500,这个值非常小,因为命令花费更多时间 execute.So 将其更改为更高的值。
session.setTimeout(50000);
我正在尝试使用 JSCH 通过 SSH 执行多个命令 library.I 我遇到了一个奇怪的问题 issue.When 我尝试 运行 eclipse 中的程序代码完成了执行但是命令(mkdir 测试)未在 server.But 中执行,当我逐步调试命令被执行并且文件夹被 created.Can 任何人解释我在这里做错了什么?代码如下:
public static void runCommand() throws JSchException, IOException{
JSch jsch=new JSch();
Session session=jsch.getSession("user","linuxip",22);
List<String>commandList=new ArrayList<String>();
commandList.add("cd deploy");
commandList.add("mkdir test");
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
session.setTimeout(500);
System.out.println("Connecting..");
Channel channel = session.openChannel("exec");
OutputStream out = channel.getOutputStream();
((ChannelExec) channel).setCommand("sudo su - wsp ");
System.out.println("connected..");
// channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
// InputStream in = channel.getInputStream();
((ChannelExec) channel).setPty(true);
channel.connect();
channel.setOutputStream(System.out);
for(String command:commandList) {
out.write((command+"\n").getBytes());
}
out.write(("exit"+"\n").getBytes());
out.flush();
out.close();
channel.disconnect();
session.disconnect();
}}
最终发现 solution.I 犯了一个愚蠢的错误,将会话超时设置为 500,这个值非常小,因为命令花费更多时间 execute.So 将其更改为更高的值。 session.setTimeout(50000);