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

How do I connect to a server from my windows box using an id_rsa key file

100+
P: 112
I was wondering how I connect to a server, using a password, login and needing to look up a key in an id_rsa file?

I was able to log into the box before just using the password and login, but security has gotten tighter, so now we need to public/private key.
I have the keys on my pc in a file called id_rsa and id_rsa.pub.

Before, I was doing this, which worked:

Expand|Select|Wrap|Line Numbers
  1. IgnoreHostKeyVerification hostKey = new IgnoreHostKeyVerification();
  2.         SshClient ssh = new SshClient();
  3.         ssh.connect(Initializer.server, 22, hostKey);
  4.         //Authenticate
  5.         PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
  6.         passwordAuthenticationClient.setUsername(Initializer.userName);
  7.         passwordAuthenticationClient.setPassword(Initializer.password);
  8.         int result = ssh.authenticate(passwordAuthenticationClient);
  9.         if(result != AuthenticationProtocolState.COMPLETE){
  10.                 System.out.println("Login to " + Initializer.server + ":" + " " + Initializer.userName + "/" + Initializer.password + " failed");
  11.  
  12.         }
  13.  
But now, Since i have to use the keys, I have been trying a few things, but i cant seem to get it to authenticate.

This is what I have tried so far:
Expand|Select|Wrap|Line Numbers
  1. String publicKeyPath = "C:"+File.separator+"Documents and Settings"+File.separator+"dedhia"+File.separator+"Desktop"+File.separator+"id_rsa";
  2.         File key = new File(publicKeyPath);
  3.         if(key.exists())
  4.         {
  5.             System.out.println("THE FILE " + publicKeyPath + " Does Exist");
  6.         }
  7.         else{
  8.             System.out.println("THE FILE DOES NOT EXIST" + publicKeyPath + " Does NOT Exist");
  9.         }
  10.  
  11.         Thread.sleep(3000);
  12.  
  13.         SshClient ssh = new SshClient();
  14.  
  15.         SshConnectionProperties properties = new SshConnectionProperties();
  16.         properties.setHost(Initializer.server);
  17.         properties.setPort(22);
  18.              properties.setPrefPublicKey("publicKeyPath");
  19.  
  20.         System.out.println("PUBLIC KEY IS: " + properties.getPrefPublicKey());
  21.  
  22.         System.out.println("PUBLIC KEY PATH IS: " + publicKeyPath);
  23.         System.out.println("THIS IS AFTER THE SETPREFPUBLICKEY");
  24.  
  25.         ssh.connect(properties, new HostKeyVerification() {
  26.             public boolean verifyHost(String host, SshPublicKey pk)
  27.                     throws TransportProtocolException {
  28.                 // TODO Auto-generated method stub
  29.                 System.out.println("VERIFYING HOST: " + host);
  30.                 System.out.println("PK IS: " + pk);
  31.  
  32.                 return true;
  33.             }
  34.         });
  35.         System.out.println("THIS IS AFTER THE SSH.CONNECT");
  36.  
  37.         PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
  38.         passwordAuthenticationClient.setUsername(Initializer.userName);
  39.         passwordAuthenticationClient.setPassword(Initializer.password);
  40.         int result = ssh.authenticate(passwordAuthenticationClient);
  41.         System.out.println("RESULT IS: " + result);
  42.         if(result != AuthenticationProtocolState.COMPLETE){
  43.                 System.out.println("Login to " + Initializer.server + ":" + " " + Initializer.userName + "/" + Initializer.password + " failed");
  44.  
  45.         }
  46.  
When i get print the result, it always comes as a 2, which is failed.

Please help!!

thanks
Oct 4 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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