I want to ssh to a machine which is behind a proxy and do a portforwarding after that in my java program. (To be able to ssh to the box I should have ssh'ed to the proxy machine first). I usually do that by having the following entries in my ~/.ssh/config file:
ProxyCommand ssh proxyhost.com -t nc %h %p
IdentityFile /home/username/username_dsa_key
And then I run the following to do portforwarding to map hostmachine.com:54321 to my localhost:12345:
ssh -A -L12345:localhost:54321 hostmachine.com
Now I want to do these with Jsch library but I can't figure out how to connect to the second host after opening the channel on the session:
String proxyHost = "proxyhost.com";
String host = "hostmachine.com";
int lport = 12345;
String rhost = "localhost";
int rport = 54321;
JSch jsch=new JSch();
jsch.setKnownHosts("/home/{user}/.ssh/known_hosts");
jsch.addIdentity("/home/{user}/.ssh/{user}_dsa_key",passphrase);
Session session1 = jsch.getSession(user,proxyHost,22);
session1.connect(3000);
System.out.println(session1.isConnected());
Channel channel = session1.openChannel("shell");
////// Now what? :)
channel.disconnect();
session1.disconnect();
Any idea?
p.s: I have read the samples in www.jcraft.com/jsch/examples/ but they didn't help in this case unfortunately.
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…