Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Connecting using Net-OpenSSH with public key instead of password

by NewMonk2Perl (Sexton)
on May 31, 2016 at 18:47 UTC ( [id://1164633]=perlquestion: print w/replies, xml ) Need Help??

NewMonk2Perl has asked for the wisdom of the Perl Monks concerning the following question:

Good morning Monks, I am having trouble trying to troubleshoot, trying to see if this even connects and if it connects, is it even running my command? Below is my code and the output I am getting. I am not sure what is doing at this point. I have tried 2 different secured FTP sites with their own public/private keys (both .pem & .ppk keys) with the code below and both gives me the same output information. The public key below actually does not have a password thus no paraphase given. I used WinSCP to connect using the same login info and public/private keys for both instance and I am able to connect and transfer files manually for each secured FTP sites. I do not understand what the output is trying to tell me. Any help would be greatly appreciated as usual:

#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; my $host = "10.92.xx.x"; my $user = "data"; my $key_path = 'C:\Scripts\dataops.pem'; my $port = "22"; $Net::OpenSSH::debug |= 16; my $ssh = Net::OpenSSH->new(host=>$host, user=>$user, port=>$port, #password=>$password, key_path => $key_path); $ssh->error and die "SSH connection failed: " . $ssh->error; $ssh->system("ls /usr/home/dataops") or die "remote command failed: " . $ssh->error;

Here is the output to STDOUT:

# open_ex: ['ssh','-V'] # killing SSH slave, pid: -18436 # waiting for slave, timeout: 0, remaining: 0, sleep: 0.1

Replies are listed 'Best First'.
Re: Connecting using Net-OpenSSH with public key instead of password
by salva (Canon) on Jun 01, 2016 at 06:45 UTC
    Net::OpenSSH does not work on Windows!

    Your options are:

    • Net::SSH2, which if you are using Strawberry Perl is already there, though I advice you to upgrade to the latest version.
    • Net::SSH::Perl, quite difficult to install in Windows.

    And then there is Net::SSH::Any, which provides an API very similar to that of Net::OpenSSH running on top of Net::SSH2.

    A much improved version of Net::SSH::Any is almost ready. You can grab it from the GitHub repository. It is more reliable, friendly and feature rich than the version available from CPAN and can also run on top of most Windows binary SSH clients, including plink.

Re: Connecting using Net-OpenSSH with public key instead of password
by Lotus1 (Vicar) on May 31, 2016 at 22:19 UTC

    Hi NewMonk2Perl

    The first thing I noticed is that you aren't calling new() the way the documentation shows. The host is either a string or a variable but not part of the hash. The options hash is the second parameter.

    my $ssh = Net::OpenSSH->new($host, %opts); $ssh->error and die "Can't ssh to $host: " . $ssh->error

    If fixing that doesn't solve your problems there is a debugging section at the end of Net::OpenSSH.

    For instance, in order to activate all the debugging flags, you can use:
    $Net::OpenSSH::debug = ~0;

    Update:

    Try changing just this one line in your program:

    my $ssh = Net::OpenSSH->new( $host, user=>$user,
Re: Connecting using Net-OpenSSH with public key instead of password
by talexb (Chancellor) on May 31, 2016 at 19:19 UTC

    If you're not sure what it's doing, run the ssh command from the command line. You can also add the debug option to the ssh command to get additional information about what it's doing in detail.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      I am running on windows with ActiveState Perl, Ok I downloaded Putty but when I login, it works just like when I use WinSCP. I am able to access the folders & files and I dont get the same message. I would like to automated this process rather than having to login and download and extract the files manually each time. Thanks for your response.

        I downloaded Putty but when I login, it works just like when I use WinSCP.

        I'm not sure if this is the problem: WinSCP uses the same key format as PuTTY, and it is different from the OpenSSH format. I assume Net::OpenSSH expects a key in OpenSSH format. The PuTTYgen program can convert between OpenSSH and PuTTY formats.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1164633]
Approved by talexb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found