Reputation: 45
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);
}
}
Upvotes: 0
Views: 1335
Reputation: 25448
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.
Upvotes: 1