在Windows上对JSch ChannelShell进行故障排除

时间:2016-09-20 15:24:31

标签: java windows ssh jsch

我使用ChannelShell向JSch发送命令 但是我在针对Windows机器上遇到麻烦。

这是代码:

package example;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import com.jcraft.jsch.*;

public class JSchShell {

    private static JSch jsch = null;
    private static String user = null;
    private static String passwd = null;
    private static String host = null;      
    static String inputCommands = "dir\r\ncd ..\r\nexit\r\n";

    public static void main(String[] args) throws Exception {

        if (args.length < 3) {
            throw new RuntimeException("Params needed: <host/ip> <user> <passwd>");
        }

        host = args[0];
        user = args[1];
        passwd = args[2];

        jsch = new JSch();

        try {
            Session session = jsch.getSession(user, host, 22);
            session.setPassword(passwd);
            session.setConfig("StrictHostKeyChecking", "no");

            long startTime = System.nanoTime();
            System.out.println("Before session.connect()");
            session.connect();
            long endTime = System.nanoTime();
            long duration = (endTime - startTime);
            System.out.println("After session.connect() - Took:"+ duration/1000000 + "ms");

            startTime = System.nanoTime();
            Channel channel = session.openChannel("shell");

            InputStream in = new ByteArrayInputStream(inputCommands.getBytes(StandardCharsets.UTF_8));

            channel.setInputStream(in);     
            channel.setOutputStream(System.out, true);

            System.out.println("Before channel.connect()");
            channel.connect();

            do {
                Thread.sleep(1);
            } while(!channel.isEOF());

            endTime = System.nanoTime();
            duration = (endTime - startTime);
            System.out.println("After commands - Took:"+ duration/1000000 + "ms");

            session.disconnect();


        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

如果我在Unix机器上运行它可以正常工作,但似乎没有对Windows机器做任何事情。

这是针对Unix机器的输出(我只是ssh到我的localhost,但它适用于其他机器):

[sysadmin@unixhost jsch]$ java -jar jschShell.jar localhost user passwd 
Before session.connect()
After session.connect() - Took:340ms
Before channel.connect()
Last login: Tue Sep 20 16:05:30 2016 from localhost
#############################################################################
dir

cd ..

exit

[sysadmin@unixhost ~]$ dir
certificados  corina  corina-curl.tgz  keys-for-mft  tom
[sysadmin@unixhost ~]$ 
[sysadmin@unixhost ~]$ cd ..
[sysadmin@unixhost home]$ 
[sysadmin@unixhost home]$ exit
logout
After commands - Took:34ms
[sysadmin@unixhost jsch]$ 

以下是Windows机器的输出:

[sysadmin@unixhost jsch]$ java -jar jschShell.jar windowshost user passwd
Before session.connect()
After session.connect() - Took:818ms
Before channel.connect()
Last login: Tue Sep 20 2016 15:58:20 +0100
After commands - Took:108ms
[sysadmin@unixhost jsch]$ 

这让我疯狂...... 我一直在测试ChannelExec,我让它在两台机器上运行。 我也使用this answer to another JSch question的方法,但我得到了类似的结果。

任何提示?

0 个答案:

没有答案