http://www.perlmonks.org?node_id=1001275

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

Venerable Monks,

I am trying to collect the ouptut of a command that is run after logging in to a NAS Array through SSH.

The command,  nas_server -list gives the statuts of the NAS Array. I am trying the following script to do it. For some reason, its not capturing the actual command output of the command in the  @output array even after stating  my @output = $chan->exec('nas_server -list'); It seems to capture the value 1, which I presume corresponds with the successful status of the command being executed.

Here is the complete script

#!/usr/bin/perl; use Modern::Perl; use 5.014; use Net::SSH2; my $nasip = '127.0.0.1'; #Actul IP is different. my $user = 'user'; my $pass = 'passwd'; my $ssh = Net::SSH2->new(); $ssh->debug(1); say "Connecting to $nasip"; $ssh->connect("$nasip") || warn "$!"; $ssh->auth_password("$user","$pass"); my $chan = $ssh->channel(); my @output = $chan->exec('nas_server -list'); say "output is: @output"; $chan->close();

And here is the output. The IP given in the example is 127.0.0.1, but I am actually using a different IP to login to the NAS Array

Connecting to 127.0.0.1 libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type +, window_size, packet_size, ((void *)0) , 0 ) -> 0x11353dc output is: 1 Net::SSH2::Channel::DESTROY Net::SSH2::DESTROY object 0x9b745c

Firstly, I know very rudimentary Perl and I am back here after almost a year, so if any mistakes in the script above please do forgive me. Thanks a lot to vdubjunkie, because If not for his node Net::SSH2 woes, I wouldn't even have written the above script.

The NAS Array (Network Attached Storage Array) is an EMC Storage Array - NS960. I am running the script from a Windows 2003 host on which Perl Version 5.14.2 is installed.

Kindly request you please guide me to a site where a detailed tutorial for the Net:SSH2 Module is given. I did go through the perldoc, also googled, but didn't find anything that I could understand. :(

Perlpetually Indebted To PerlMonks

Replies are listed 'Best First'.
Re: Unable to store the command output from Net::SSH2. Please help.
by salva (Canon) on Oct 28, 2012 at 17:19 UTC
    Net::SSH2::Channel::exec does not capture the output of the remote command, it just launches it. You have to use the read method afterward to retrieve the output.

    On Unix/Linux systems Net::OpenSSH is far easier to use. Or there is also Net::SSH::Any that can work on top of Net::SSH2 and provides a simpler to use API, though it is still not a mature module.

Re: Unable to store the command output from Net::SSH2. Please help.
by syphilis (Archbishop) on Oct 29, 2012 at 09:21 UTC
    Based on what works for me on Windows (connecting to a linux server) you should be able to do:
    my @output; $chan->blocking(1); $chan->exec('nas_server -list'); while (<$chan>) {push @output, $_} print for @output;
    Cheers,
    Rob
Re: Unable to store the command output from Net::SSH2. Please help.
by perl514 (Pilgrim) on Oct 29, 2012 at 14:54 UTC

    Hi Salva,

    Thanks for the update, but I cannot download any extra modules, because the system from which I am running the perl scripts is not connected to the internet. I am using DWIM Perl and it only has Net::SSH2.

    Hi Syphilis (Rob),

    I did try your suggestion, but that still doesnt work. All I got is whats shown below

    Connecting to xxx.xxx.xxx.xxx libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type +, window_size, packet_size, ((void *)0) , 0 ) -> 0x1137a74 Net::SSH2::poll: timeout = 250, array[1] - [0] = channel - [0] events 1 - libssh2_poll returned 1 - [0] revents 128 Net::SSH2::Channel::DESTROY Net::SSH2::DESTROY object 0x9b7404

    Any suggestion to a good tutorial for Net::SSH2 will really help.

    Perlpetually Indebted To PerlMonks

      Well, Net::SSH::Any has no dependencies besides Net::SSH2, so it should be pretty easy to install, even without internet access. Just unpack its lib directory in the right place under the DWIN directory and you are done!
Re: Unable to store the command output from Net::SSH2. Please help.
by perl514 (Pilgrim) on Nov 06, 2012 at 18:56 UTC

    Hi

    Tried using the Net::SSH::Any module, but it's not doing what I expected. I am going wrong somewhere.

    I am trying to ssh into the VMWare Guest OS (CentOS 6.2). Before trying the script, I pinged and ensured that its working. I am also able to putty into the VMWare Guest OS, but not able to use the script for some reason.

    Given below is the script

    #!/usr/bin/perl use Modern::Perl; use Net::SSH::Any; my $host = '192.168.247.101'; my $user = "perl514"; my $passwd = "redhat"; my $ssh = Net::SSH::Any->new ($host, user => $user, password => $passw +d); my @output = $ssh->capture2("ls -l"); say "@output";

    And here is the output. The cursor blinks for a while so it appears that it could be running the command, but returns the following error

    Use of uninitialized value $output[0] in join or string at anyping.pl +line 10.

    Basically I want to capture the output of the ls -l command in the @output.

    Perlpetually Indebted To PerlMonks