I am not able to ssh from Windows 7 to VMWare Guest CentOS 6.2
I have Windows 7 64 bit OS on my Laptop. Installed Strawberry Perl 5.16.2 on it. I have also installed VMWare Player and running CentOS 6.2 on it.
Using the articles I found on the net, I have been successfully able to SSH without password from PUTTY to the CentOS 6.2.
I have created user perl514 on CentOS 6.2. Using the script below, if I login using the username and password, everything works. But it doesn't work with the Public and Private Keys that I created using the SSH KEYGEN Tool that came along with the PUTTY.
Given below is the script:
#!C:\strawberry\perl\bin\perl.exe
use Modern::Perl;
use Net::SSH2;
my $hostname = '192.168.247.128';
my $username = 'perl514';
my $password = 'redhat';
my $ssh2 = Net::SSH2->new();
say "Connecting to $hostname";
$ssh2->connect("$hostname") || die "PROBELM - $!";
#$ssh2->auth_password("$username","$password") || die "Username/Password not #right";#COMMENTED OUT. This Works. Stuff given below does not work.
$ssh2->auth_publickey ("perl514", "C:\\Users\\winuser\\Documents\\perl\\work\\putty_priv.ppk",
"C:\\Users\\winuser\\Documents\\perl\\work\\public.pub") || die "ERROR", $ssh2->error;
my $chan = $ssh2->channel();
$chan->blocking(0);
$chan->exec('ls -la');
while (<$chan>){ print }
I get the following error:
Connecting to 192.168.247.128
ERROR-19LIBSSH2_ERROR_PUBLICKEY_UNVERIFIEDInvalid public key at ssh2.pl line 17.
With the username and password, it works fine. But not with the public and private key.
I am pretty sure I am going wrong somewhere. Kindly help me.
Net::SSH2 expects the key files to be in the OpenSSH format that is different to that used by PuTTY.
You should be able to convert to OpenSSH format using the PuTTY GUI. For instance, see How to convert SSH keypairs generated using PuttyGen(Windows) into key-pairs used by ssh-agent and KeyChain(Linux) .
update:
Detailed conversion steps:
- Open PuTTY key generator
- Load the private key into it
- On the conversions menu, export the private key in OpenSSH format
- Select and copy with the mouse the public key for OpenSSH from the box under "Public key for pasting into OpenSSH authorized_keys file" and paste it into a new file with the same name as the one you have given to the private key with ".pub" appended.