Discussion:
[JSch-users] Connect to sub machine via SSH and Jsch
Mohamed Boussaa
2015-12-18 14:22:17 UTC
Permalink
I am trying to connect in Java to my machines in the cloud using JSCH.

My machine's IP is 10.0.0.1.

In this host machine I want to connect to a nested machine with IP 10.0.0.22.

Actually I am connecting to the 10.0.0.1 machine using the following code:

private static String user = "ubuntu";
private static String host = "10.0.0.1";
private static String password = "mypass";
private static String command = "ls";

public static void main(String args[]) throws JSchException,
InterruptedException
{
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect(10*1000);
Channel channel = session.openChannel("shell");
InputStream is = new ByteArrayInputStream(command.getBytes());
channel.setInputStream(is);
channel.setOutputStream(System.out);
channel.connect(15 * 1000);
Thread.sleep(3*1000);


channel.disconnect();
session.disconnect();
}

My question is how could I open a new connection to the machine
10.0.0.22 inside the 10.0.0.1's machine using JSCH API?

------------------------------------------------------------------------------
Shai Ayal
2015-12-18 14:56:38 UTC
Permalink
Maybe use port forwarding ?
Post by Mohamed Boussaa
I am trying to connect in Java to my machines in the cloud using JSCH.
My machine's IP is 10.0.0.1.
In this host machine I want to connect to a nested machine with IP 10.0.0.22.
private static String user = "ubuntu";
private static String host = "10.0.0.1";
private static String password = "mypass";
private static String command = "ls";
public static void main(String args[]) throws JSchException,
InterruptedException
{
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect(10*1000);
Channel channel = session.openChannel("shell");
InputStream is = new ByteArrayInputStream(command.getBytes());
channel.setInputStream(is);
channel.setOutputStream(System.out);
channel.connect(15 * 1000);
Thread.sleep(3*1000);
channel.disconnect();
session.disconnect();
}
My question is how could I open a new connection to the machine
10.0.0.22 inside the 10.0.0.1's machine using JSCH API?
------------------------------------------------------------------------------
_______________________________________________
JSch-users mailing list
https://lists.sourceforge.net/lists/listinfo/jsch-users
Mohamed Boussaa
2015-12-18 15:06:54 UTC
Permalink
Post by Shai Ayal
Maybe use port forwarding ?
How?

------------------------------------------------------------------------------
Bogdan SOLGA
2015-12-18 15:26:42 UTC
Permalink
Post by Shai Ayal
Maybe use port forwarding ?
How?
http://www.jcraft.com/jsch/examples/PortForwardingR.java.html +
http://www.jcraft.com/jsch/examples/PortForwardingL.java.html
Mohamed Boussaa
2015-12-18 15:46:45 UTC
Permalink
I did that but I got "connection refused"



StringBuilder outputBuffer = new StringBuilder();


String host="10.0.0.1"; // First level target

String user="user1";

String password="pass1";

String tunnelRemoteHost="10.0.0.22"; // The host of the second target

String secondPassword="pass2";

int port=22;



JSch jsch=new JSch();

Session session=jsch.getSession(user, host, port);

session.setPassword(password);

localUserInfo lui=new localUserInfo();

session.setUserInfo(lui);

session.setConfig("StrictHostKeyChecking", "no");

// create port from 2233 on local system to port 22 on tunnelRemoteHost

session.setPortForwardingL(2251, tunnelRemoteHost, 22);

session.connect();

session.openChannel("direct-tcpip");


// create a session connected to port 2233 on the local host.

Session secondSession = jsch.getSession("user2",
tunnelRemoteHost, 2251);

secondSession.setPassword(secondPassword);

secondSession.setUserInfo(lui);

secondSession.setConfig("StrictHostKeyChecking", "no");


secondSession.connect(); // now we're connected to the secondary system

Channel channel=secondSession.openChannel("exec");

((ChannelExec)channel).setCommand("hostname");


channel.setInputStream(null);


InputStream stdout=channel.getInputStream();


channel.connect();

------------------------------------------------------------------------------
Hajo Passon
2015-12-18 16:02:04 UTC
Permalink
Am Fri, 18 Dec 2015 16:46:45 +0100
Post by Mohamed Boussaa
I did that but I got "connection refused"
StringBuilder outputBuffer = new StringBuilder();
String host="10.0.0.1"; // First level target
String user="user1";
String password="pass1";
String tunnelRemoteHost="10.0.0.22"; // The host of the second target
String secondPassword="pass2";
int port=22;
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, port);
session.setPassword(password);
localUserInfo lui=new localUserInfo();
session.setUserInfo(lui);
session.setConfig("StrictHostKeyChecking", "no");
// create port from 2233 on local system to port 22 on
tunnelRemoteHost
session.setPortForwardingL(2251, tunnelRemoteHost, 22);
session.connect();
session.openChannel("direct-tcpip");
// create a session connected to port 2233 on the local host.
Session secondSession = jsch.getSession("user2",
tunnelRemoteHost, 2251);
I didn't test or compile anything, but I think this line is wrong.

You opened up your tunnel port on the FIRST host. So you have to
connect to your first host (ip 10.0.0.1 - called "host" in your code)
to reach the second one by tunnel.

regards
Hajo
Post by Mohamed Boussaa
secondSession.setPassword(secondPassword);
secondSession.setUserInfo(lui);
secondSession.setConfig("StrictHostKeyChecking", "no");
secondSession.connect(); // now we're connected to the
secondary system
Channel channel=secondSession.openChannel("exec");
((ChannelExec)channel).setCommand("hostname");
channel.setInputStream(null);
InputStream stdout=channel.getInputStream();
channel.connect();
------------------------------------------------------------------------------
_______________________________________________
JSch-users mailing list
https://lists.sourceforge.net/lists/listinfo/jsch-users
------------------------------------------------------------------------------
Mohamed Boussaa
2015-12-18 16:35:10 UTC
Permalink
can you give me an example according to my settings?

------------------------------------------------------------------------------
Loading...