jcraft jsch +在远程机器上执行命令

时间:2012-12-09 22:24:23

标签: java exception jsch

尝试在远程计算机上运行命令(例如:检查文件是否存在)但收到此错误。在这一步失败 -

  

channel.connect(60000); com.jcraft.jsch.JSchException:无法发送   来自com.jcraft.jsch.Request.write(Request.java:65)的频道请求   com.jcraft.jsch.RequestExec.request(RequestExec.java:56)at   com.jcraft.jsch.ChannelExec.start(ChannelExec.java:43)at at   com.jcraft.jsch.Channel.connect(Channel.java:152)at   SampleTest.jcraftTest(SampleTest.java:143)at   SampleTest.main(SampleTest.java:23)

我正在使用的代码

JSch jsch = new JSch();
session = jsch.getSession("username", "host", 22);

Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "password,keyboard-interactive");

session.setConfig(config);
session.setPassword("password");
session.connect(60000);

System.out.println("session connected");

channel = session.openChannel("exec");

System.out.println("channel opened");

((ChannelExec) channel).setCommand("uname -a && date && uptime && who");
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect(60000);

System.out.println("channel connected");

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));
    }
    if (channel.isClosed()) {
        System.out.println("exit-status: " + channel.getExitStatus());
        break;
    }
    try {
        Thread.sleep(1000);
    } catch (Exception ee) {
        ee.printStackTrace();
    }
}

0 个答案:

没有答案