Using SSH in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pkpalanisamy
    New Member
    • Mar 2012
    • 7

    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.Connec tException: Connection refused: connect
    SSH Socket creation failed java.net.Connec tException: Connection refused: connect
    SSH Socket creation failed java.net.Connec tException: Connection refused: connect
    SSH Socket creation failed java.net.Connec tException: Connection refused: connect
    SSH Socket creation failed java.net.Connec tException: Connection refused: connect
    SSH Tunneling failed - falling back to insecure connection*****

    Java code:
    Code:
    String sshCommand = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -q -N -T -L"+ portNo +":" + address + ":" + port + " svc_pos_client@" + address + " &" ;
    try
    {
      System.out.println( "createSSHTunnel() - Creating ssh tunnel with " + sshCommand);
      //Runtime.getRuntime().exec(sshCommand);
      Process process=Runtime.getRuntime().exec(sshCommand);
    }
    could any one please help me out?
    Thanks
    Last edited by Frinavale; Mar 19 '12, 04:29 PM. Reason: Added code tags, markup for indicating the error message, and added indentation to posted code.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • pkpalanisamy
      New Member
      • Mar 2012
      • 7

      #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...

      Comment

      • pkpalanisamy
        New Member
        • Mar 2012
        • 7

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

        Thank you...

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          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.

          Comment

          • pkpalanisamy
            New Member
            • Mar 2012
            • 7

            #6
            see below script and java files:
            script:
            #!/bin/bash
            ssh -o UserKnownHostsF ile=/dev/null -o StrictHostKeyCh ecking=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.getRunt ime().exec(cmd) ;
            BufferedReader input =
            new BufferedReader( new InputStreamRead er(p.getInputSt ream()));
            while ((line = input.readLine( )) != null) {
            if(line.trim(). length() > 0)
            {
            System.out.prin tln(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?

            Comment

            • pkpalanisamy
              New Member
              • Mar 2012
              • 7

              #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 'createSShTunne l.sh 55557 10.236.250.182 6301'";
              int portNo=55557;
              String address="10.236 .250.182";
              int port=6301;
              UserKnownHostsF ile=/dev/null -o StrictHostKeyCh ecking=no -q -N -T -L"+ portNo +":" + address + ":" + port + " svc_pos_client@ " + address + " &" ;
              File file=new File("bin/createSShTunnel .sh");
              String cmd="sh "+ file.getAbsolut ePath().toStrin g()+ " 55557 10.236.250.182 6301";//+portNo+" "+address+" "+port;
              String line;
              Process p = Runtime.getRunt ime().exec(cmd) ;
              BufferedReader input =new BufferedReader( new InputStreamRead er(p.getInputSt ream()));
              while ((line = input.readLine( )) != null)
              {
              System.out.prin tln("in");
              if(line.trim(). length() > 0)
              {
              System.out.prin tln("output: "+line.trim ());
              }
              }
              }
              catch(Exception e)
              {
              System.out.prin tln("Exception: "+e);
              }

              Comment

              Working...