简体   繁体   中英

how to create ssh connection via socks proxy on local port in java

I have a socks proxy and can create ssh connection in terminal of Ubuntu on local port by this command:

ssh -D local_port user_name@host_IP

but I want perform this task through Java code. I used the code below, but any time run it, show this exception:

"com.jcraft.jsch.JSchException: ProxySOCKS5: com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused"

public static void main(String[] arg) {
        try {
            JSch jsch = new JSch();
            String user = "user_name";
            String host = "host_IP";
            int port = local_port;
            Session session = jsch.getSession(user, host, 22);
            Proxy proxy = new ProxySOCKS5(host, port);
            session.setProxy(proxy);
            String passwd = "password";
            session.setPassword(passwd);
            UserInfo ui = new MyUserInfo()
            session.setUserInfo(ui);
            session.connect(30000);
            Channel channel = session.openChannel("shell");
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);
            channel.connect(3 * 1000);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

The ProxySOCKS5 class implements a SOCKS client which Jsch can use to make an the original SSH connection.

To provide the equivalent of ssh's -D option, you would need a SOCKS server which opened a ChannelDirectTCPIP channel for each client. Jsch doesn't provide such a thing, so you may have to write your own .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM