Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Capturing SSH output in an array.

by perl514 (Pilgrim)
on Nov 14, 2012 at 13:31 UTC ( #1003801=perlquestion: print w/ replies, xml ) Need Help??
perl514 has asked for the wisdom of the Perl Monks concerning the following question:

Respected Monks,

I am looking for a way to ssh into a NAS Array (linux based) from a Windows Server and capture the output into an array. Is there a way to do that? I tried using Net::SSH2 as well as Net::SSH::Any, but I am going wrong somewhere. Please see Unable to store the command output from Net::SSH2. Please help.. Kindly guide me.

Perlpetually Indebted To PerlMonks

Comment on Capturing SSH output in an array.
Re: Capturing SSH output in an array.
by zentara (Archbishop) on Nov 14, 2012 at 14:29 UTC
    You really need to provide more information. You already have received answers from the best experts in your link.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Capturing SSH output in an array.
by salva (Abbot) on Nov 14, 2012 at 14:54 UTC
    There was a silly bug on Net::SSH::Any breaking password authentication. It is now corrected on version 0.02 that I have just uploaded to CPAN.
    use Net::SSH::Any; my $ssh = Net::SSH::Any->new($host, user => $user, password => $passwo +rd); my @out = $ssh->capture($cmd);
Re: Capturing SSH output in an array.
by perl514 (Pilgrim) on Nov 14, 2012 at 19:01 UTC

    Hi Salva !!!

    Pity I can vote this only once. I installed your new updated module and it just works now !!!. Here is the script

    #!/usr/bin/perl use Modern::Perl; use Net::SSH::Any; my $hostname = '192.168.247.128'; my $username = 'perl514'; my $password = 'redhat'; my $cmd = 'ls -l'; if (my $ssh = Net::SSH::Any->new($hostname, user => $username, passwor +d => $password)) { say " I am connected to $hostname!!!"; my @out = $ssh->capture($cmd); say "The files and directories of $username on $hostname are given + below"; say "@out"; } else { say "whee...something wrong here" }

    Aand here is the output !!

    I am connected to 192.168.247.128!!! The files and directories of perl514 on 192.168.247.128 are given belo +w total 16 drwxrwxr-x. 2 perl514 perl514 4096 Nov 14 14:52 perlscripts drwxrwxr-x. 2 perl514 perl514 4096 Nov 14 14:52 test1 drwxrwxr-x. 2 perl514 perl514 4096 Nov 14 14:52 test2 drwxrwxr-x. 2 perl514 perl514 4096 Nov 14 14:52 test3

    You rock !!

    Thing is, it took about 12 seconds for the script to ssh into the Centos VM Guest OS, but man I am glad this works !!...Is there a way to save the ssh keys and circumvate the password requirement? If the Net::SSH2 method will work (I still havent tried it) to add the ssh keys, then I'll try that out. But yeah, this works !! O yeah, by the way, I finally got rid of Windows 7 on my personal laptop....will try on that one too !! Its running Ubuntu 12.04 now, so itching to try out stuff on it :) So for work, I will be on Win 7 (And to manage the storage environment it will be Win 2003: Not my choice), but on personal laptop, its Linux all the way now.

    Perlpetually Indebted To PerlMonks

      Net::SSH::Any is still a work on progress and the documentation is quite lacking, but the API is mostly a subset of that of Net::OpenSSH (IIRC, new, system, capture, capture2, error and scp methods are supported) so you should read its docs.

      The new method returns always an object, even when the connection fails. You have to use the error method to check for errors.

      In order to use public key authentication, use the constructor option key_path to pass the path of the file containing the private key to Net::SSH::Any.

      my $ssh = Net::SSH::Any->new($hostname, user => $user, key_path => "/h +ome/$user/.ssh/id_dsa"); if ($ssh->error) { say "whee...something wrong here: " . $ssh->error; } else { say " I am connected to $hostname!!!"; my @out = $ssh->capture($cmd); say "The files and directories of $username on $hostname are given + below"; say "@out"; }

      Update: BTW, the module should be as fast as any other SSH client implementation (i.e. PuTTY or OpenSSH). Connecting to some machine in the same (or a near) LAN should be a sub-second operation.

      Maybe sshd is configured on the Centos machine to use ident or your DNS configuration is broken or you have any other network problem or the host supporting the VM is overloaded.

Re: Capturing SSH output in an array.
by perl514 (Pilgrim) on Nov 20, 2012 at 16:06 UTC

    Hi Salva,

     

    The script works fine if I include the username and password, but fails when I use the key_path => "/home/$user/.ssh/id_dsa"); option.

    Here's the script:

    #!/usr/bin/perl use Modern::Perl; use Net::SSH::Any; my $hostname = '192.168.247.128'; my $username = 'perl514'; my $password = 'redhat'; say "Trying to connect to $hostname . Might take a while"; my $ssh = Net::SSH::Any->new($hostname, user => $username, key_path => + "/home/$username/.ssh/id_dsa"); if ($ssh->error) { say "whee...something wrong here: " . $ssh->error; } else { say " I am connected to $hostname!!!"; my @out = $ssh->capture("ls -la"); say "The files and directories of $username on $hostname are given + below"; say "@out"; }

    And here is the error message

    perl anytest1.pl Trying to connect to 192.168.247.128 . Might take a while whee...something wrong here: Authentication failed

    This is slightly modified version of your script and I am running this on my office laptop that has Windows 7 installed with DWIM Perl. The script is pointing to CentOS 6.2 running as Guest OS inside of VMWare Player. I am able to ping to it as well as putty into it, so no issues there.I thought that there could be issues with the way  $username is being interpolated, but that's not the issue. The /home/$user/.ssh/id_dsa refers to id_dsa inside the CentOS 6.2. I am able to login to the Guest OS using the same username and password mentioned in the script, so any issues due wrong username and password are ruled out as well.

    So I tried similar stuff using the  Net::SSH2 module, because from what I understand,  Net::SSH::Any module would use either  Net::OpenSSH or  Net::SSH2 in the backend. tried with  Net::SSH2 and same error was observed. Not sure where I am going wrong. Please help me.

    Perlpetually Indebted To PerlMonks

      The argument to key_path is the place where the private key is located in the local machine (in your case, the Windows 7 host).

      And BTW, you should write your replies attached to the node you are actually replying, not under the root query node. Otherwise the monk you are replying to may not notice it.

        Hi Salva,

         

        I earlier used to reply to the person who answered, but was told to reply to the root thread, but I see your point and hence replying to you.

        It was pretty dumb of me not to understand where the key_path is coming from. I googled up and found a few solutions to set up ssh keys working in Windows. Will surely keep you posted.

        Perlpetually Indebted To PerlMonks

        W7 at Work, SL6.3 at home.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (17)
As of 2014-12-18 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (58 votes), past polls