如何测试Java SSH客户端jsch?

时间:2018-04-18 08:45:49

标签: java ssh

我已经分配了关于Java SSH客户端(jsch)的内容。 我读了几个例子,但是当我尝试其中一些时,我总是得到同样的错误。 拒绝连接:连接 我可以尝试在我的主机上,只是为了测试目的。我是新手。只是想要没有任何错误。 这是我尝试的例子:

public static void main(String[] arg) {
    try {
        JSch jsch = new JSch();

        String user = "username";
        String host = "host ip";
        int port = 22;
        String privateKey = ".ssh/id_rsa";

        jsch.addIdentity(privateKey);
        System.out.println("identity added ");

        Session session = jsch.getSession(user, host, port);
        System.out.println("session created.");

         java.util.Properties config = new java.util.Properties();
         config.put("StrictHostKeyChecking", "no");
         session.setConfig(config);

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

        Channel channel = session.openChannel("sftp");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect();
        System.out.println("shell channel connected....");

        ChannelSftp c = (ChannelSftp) channel;

        String fileName = "test.txt";
        c.put(fileName, "./in/");
        c.exit();
        System.out.println("done");

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

1 个答案:

答案 0 :(得分:0)

"Connection refused" means that the client sent a TCP connection request to the server, and it received a response from the server refusing request.

The simplest explanation is that there is nothing listening for connections on the address and port that the client tried to connect to. In this case, it would mean that there's no SSH server process running on the remote server, or it's running but it not configured to listen for connections on the correct address and port.

Alternately, "connection refused" could mean that a firewall or some similar network device is blocking the connection requests from the client.