Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java code to connect to remote windows machine from local machine

2610381Apr 11 2014 — edited Apr 22 2014

Hi,

I have developed a code to connecting remote windows M/C from local M/C by using SSH2 (ganymed-ssh2-build209.jar) API. when I run the code its giving below error. Can any one please help me how to resolve it. And also please let me know, is there any other way to connect remote windows system using java code.

Exception.

java.io.IOException: There was a problem while talking to <host name>:22

  at ch.ethz.ssh2.Connection.connect(Connection.java:642)

  at ch.ethz.ssh2.Connection.connect(Connection.java:460)

  at Connect.RemoteServer.ConnectWindowsServer.runCommand(ConnectWindowsServer.java:55)

  at Connect.RemoteServer.ConnectWindowsServer.main(ConnectWindowsServer.java:27)

Caused by: java.net.ConnectException: Connection refused: connect

  at java.net.PlainSocketImpl.socketConnect(Native Method)

  at java.net.PlainSocketImpl.doConnect(Unknown Source)

  at java.net.PlainSocketImpl.connectToAddress(Unknown Source)

  at java.net.PlainSocketImpl.connect(Unknown Source)

  at java.net.SocksSocketImpl.connect(Unknown Source)

  at java.net.Socket.connect(Unknown Source)

  at ch.ethz.ssh2.transport.TransportManager.initialize(TransportManager.java:299)

  at ch.ethz.ssh2.Connection.connect(Connection.java:591)

  ... 3 more

JAVA Code

import ch.ethz.ssh2.Connection;

import ch.ethz.ssh2.Session;

public void setAuthenticationInfo(String hostname, String username,String password) {

       this.host = hostname;

       this.userid = username;

       this.password = password;      

       this.recentCommand = "";     

       System.out.println("setting authentication info completed for host=" + host );

  }

  public void runCommand() throws Exception {

try{

        // Setup ssh session with endpoint

       System.out.println("starting connection with " + host);

       Connection connection = new Connection(host);

       System.out.println("connection object created..");

       connection.connect();

       System.out.println("Connect to connection");

       connection.authenticateWithPassword(userid,password);

       System.out.println(connection.isAuthenticationComplete());

       Session session = connection.openSession();

      System.out.println("connected");

  }catch (Exception e) {

  e.printStackTrace();

  }

  }

Regards,

Praveen

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 20 2014
Added on Apr 11 2014
2 comments
14,332 views