By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

Using SSH in Java

P: 7
I have to forward my data from one port to another port by secure way(ssh packet). This should happen from local machine to server machine via port. so I used ssh tunnel to achieve this.

The logic behind is send the command to create SSH tunnel from client to server, create socket and then try connect the same. so when i use below command through command prompt to create ssh tunnel and then everything fine if i run same command in java code(which i pasted below)then it's throwing below exception

SSH Socket creation failed java.net.ConnectException: Connection refused: connect
SSH Socket creation failed java.net.ConnectException: Connection refused: connect
SSH Socket creation failed java.net.ConnectException: Connection refused: connect
SSH Socket creation failed java.net.ConnectException: Connection refused: connect
SSH Socket creation failed java.net.ConnectException: Connection refused: connect
SSH Tunneling failed - falling back to insecure connection*****
Java code:
Expand|Select|Wrap|Line Numbers
  1. String sshCommand = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -q -N -T -L"+ portNo +":" + address + ":" + port + " svc_pos_client@" + address + " &" ;
  2. try
  3. {
  4.   System.out.println( "createSSHTunnel() - Creating ssh tunnel with " + sshCommand);
  5.   //Runtime.getRuntime().exec(sshCommand);
  6.   Process process=Runtime.getRuntime().exec(sshCommand);
  7. }
could any one please help me out?
Thanks
Mar 19 '12 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
Possibly the PATH settings that you have when you run the command are different from the ones you have when you run the java command.
In this case it looks like settings to do with network connections (perhaps some proxy settings).
It's better to write an os script that calls the command successfully exporting all required environment settings and just make Runtime.exec call the script.
Mar 19 '12 #2

P: 7
Thanks fir your valuable reply.
Could you or any one can send me some sample code how to go ahead with this?

Thank you...
Mar 20 '12 #3

P: 7
Thanks fir your valuable reply.
Could you or any one can send me some sample code how to go ahead with this?

Thank you...
Mar 20 '12 #4

10K+
P: 13,264
Create a bash script that imports all the user environment variables that you need for invoking the command. Change the java program to call that bash script instead of calling the command directly. No sample code required for this.
Mar 20 '12 #5

P: 7
see below script and java files:
script:
#!/bin/bash
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -q -N -T -L"${1}":"${2}":"${3}" svc_pos_client@"${2}" &

java:
String cmd="D:\\cygwin\\bin\\bash -c './createSShTunnel.sh 55557 10.236.250.182 6301'";
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
if(line.trim().length() > 0)
{
System.out.println(line.trim());
}
}

After the above implementation i ran the java class and still it throws the same exception.
could you or any one suggest me where is wrong?
Mar 20 '12 #6

P: 7
Hi,
I had implemented the below logic and still with creating problem SSH tunnel. i can able to see ssh in task manager but when i see by using netstart. its not showing there as listening.
Could any one please help me out?

try
{
//String cmd="c:\\cygwin\\bin\\bash -c 'createSShTunnel.sh 55557 10.236.250.182 6301'";
int portNo=55557;
String address="10.236.250.182";
int port=6301;
UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -q -N -T -L"+ portNo +":" + address + ":" + port + " svc_pos_client@" + address + " &" ;
File file=new File("bin/createSShTunnel.sh");
String cmd="sh "+ file.getAbsolutePath().toString()+ " 55557 10.236.250.182 6301";//+portNo+" "+address+" "+port;
String line;
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
{
System.out.println("in");
if(line.trim().length() > 0)
{
System.out.println("output: "+line.trim());
}
}
}
catch(Exception e)
{
System.out.println("Exception: "+e);
}
Mar 22 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.