473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Using SSH in Java

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
6 5493
r035198x
13,262 8TB
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
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
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
r035198x
13,262 8TB
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
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
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

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

Similar topics

2
by: webstar | last post by:
How do you play wave files using java.applet.Applet?
1
by: ochulus | last post by:
Hi! I'm new to Java and I would like to know how to manage files (e.g. rm, mv, mkdir, etc.) using Java for a simple text-based application on Unix. It's just an extension of a tutorial question at...
1
by: Utada P.W. SIU | last post by:
Hi all professional I would like to know how to using java class file inside ASP page I have placed the class file to C:\WINNT\java\trustlib also, I have compiled it to 1.1 version but it...
10
by: asj | last post by:
BIG news from the web services front. Amazon will use web services to tie all its vendors together. The company implementing the system will be using Java/C++ (migrating to all-java later). ...
2
by: Mark Richards | last post by:
An applet on one of my pages uses a big picture as background. I want to load it before the applet and the rest of the html source. By doing this I want to avoid that the pane of the applet is...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
8
by: dbaplusplus | last post by:
I worked on web development using java script many many years, so I am now a newbie to javascript. I have a single html page, which is generated dynamically using some programming language. Web...
2
by: asenthil | last post by:
i'm trying to read and write files using java... some errors occurs when i'm trying this code.. Error in java: Cannot find symbol location: class java.io.FileOutputStream FileOutputStream...
4
by: BibhuAshish | last post by:
Hi, I wanted to delete a line from xml file which i did it. But after deletion of that line there is a blank space. Again if i am adding another line by using java that blank line remains as...
1
by: Shun | last post by:
Hello, I am trying to extract the data from my Oracle 10g database using Java. First, I tried to connect Oracle to Java using this code and it works. import java.util.*; import java.sql.*;...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.