Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Am I using Net::SSH2 the wrong way?

by perl514 (Pilgrim)
on Jan 18, 2013 at 05:16 UTC ( #1013952=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 almost done with writing a script for running a specific command on our EMC NAS Arrays which return the output of the command E-mail. The script is given at Seeking guidance for more idiomatic way of (re)writing this script.

The problem is, sometimes the script returns empty fields for some nas arrays.The ouptut looks like this:

----------------------------------------------------- Data Mover Check For nasarrayname1 (127.0.0.1) ----------------------------------------------------- DM is OK: 5 - slot_2 contacted DM is OK: 5 - slot_3 contacted DM is OK: 5 - slot_4 contacted DM is OK: 5 - slot_5 contacted ----------------------------------------------------- Data Mover Check For nasarrayname2 (127.0.0.2) ----------------------------------------------------- ----------------------------------------------------- Data Mover Check For nasarrayname3 (127.0.0.3) ----------------------------------------------------- DM is OK: 5 - slot_2 contacted DM is OK: 5 - slot_3 contacted DM is OK: 5 - slot_4 contacted DM is OK: 5 - slot_5 contacted

As shown above, for the array "nasarrayname2" the output is empty. Earlier, this was happening a lot. So I added a sleep command. Now the frequency is reduced but it still happens and it happens for different arrays. When I re run the script manually (currently its running through Windows Scheduler), the nasarray seems to return the output just fine.

----------------------------------------------------- Data Mover Check For nasarrayname1 (127.0.0.1) ----------------------------------------------------- DM is OK: 5 - slot_2 contacted DM is OK: 5 - slot_3 contacted DM is OK: 5 - slot_4 contacted DM is OK: 5 - slot_5 contacted ----------------------------------------------------- Data Mover Check For nasarrayname2 (127.0.0.2) ----------------------------------------------------- DM is OK: 5 - slot_2 contacted DM is OK: 5 - slot_3 contacted DM is OK: 5 - slot_4 contacted DM is OK: 5 - slot_5 contacted ----------------------------------------------------- Data Mover Check For nasarrayname3 (127.0.0.3) ----------------------------------------------------- DM is OK: 5 - slot_2 contacted DM is OK: 5 - slot_3 contacted DM is OK: 5 - slot_4 contacted DM is OK: 5 - slot_5 contacted

Another concern is that the script is supposed to send out a mail if the ouptut does not have certain text, but in this case, it didn't send out any mail.

Given below is some portion of the script:

my $username = 'username'; my $password = 'password'; my $ssh2 = Net::SSH2->new(); print $mailfh "\n----------------------------"; print $mailfh "\nData Mover Check For $ipname ($ipaddr)\n"; print $mailfh "----------------------------\n"; $ssh2->connect("$ipaddr") || die "PROBELM -$!"; $ssh2->auth_password("$username","$password") || die "Username +/Password not right"; my $chan = $ssh2->channel(); $chan->blocking(0); $chan->exec('/nas/sbin/getreason'); sleep 3; while (<$chan>) { chomp; next if (/10 - slot_0 primary control station/); if ($_ =~ /contacted$/) { print $mailfh "DM is OK: $_\n"; } else { print $mailfh "POSSIBLE DM FAILURE:Please check $ipname ($ +ipaddr): $_ POSSIBLE DM FAILURE:\n"; }

I am not able to understand where I'm going wrong. Should I increase the sleep time? or do I need to run Net::SSH2 output in a different way?

Please help me.

 

Perlpetually Indebted To PerlMonks

Win 7 at Work. Living through it....Linux at home. Loving it.

Comment on Am I using Net::SSH2 the wrong way?
Select or Download Code
Re: Am I using Net::SSH2 the wrong way?
by salva (Abbot) on Jan 18, 2013 at 11:04 UTC
    Well, just letting some time elapse in order to ensure that all the data has arrived is not a reliable approach.

    Is there any reason for setting the connection in non-blocking mode?

    In any case, you can try using Net::SSH::Any instead of Net::SSH2. It can run on top of Net::SSH2 and provides a higher API, making most tasks easier to accomplish. For instance:

    use Net::SSH::Any; my $ssh = Net::SSH::Any->new($ipaddr, user => username, password => $p +assword); $ssh->error and die "Unable to connect to $ipaddr: " . $ssh->error; my @lines = $ssh->capture('/nas/sbin/getreason'); for (@lines) { next if /10 - slot_0 primary control station/; if (/contacted$/) { print $mailfh "DM is OK: $_\n"; } else { print $mailfh "POSSIBLE DM FAILURE:Please check $ipname ($ipaddr): + $_ POSSIBLE DM FAILURE:\n"; } }

      Hi Salva,

      First of all, thanks for replying with such consistency. You've been replying to all my Net::SSH2 questions.

      As suggested in earlier post, I really dont have the permission to add any extra modules there on my company's Win 2003 Server. It was by pure luck that it happens to have DWIM Perl installed there. But I cannot add any modules there. Cannot even copy files. Hence cannot use the module you suggest

      The module you suggested runs on top of Net::SSH2, so if Net::SSH2 is not able to run the command, then any module that runs on top of that might have same issue.

      Perlpetually Indebted To PerlMonks

      use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";

        Net::SSH2 should be able to run the command. Problem is that getting it do it correctly may be a difficult task that is already solved in Net::SSH::Any.

        You could use its source code (look for the __io3 function) for inspiration, though it is quite convoluted at that level because of all the extra features it supports.

Re: Am I using Net::SSH2 the wrong way?
by perl514 (Pilgrim) on Jan 18, 2013 at 12:10 UTC

    Hi Salva,

    First of all, thanks for replying with such consistency. You've been replying to all my Net::SSH2 questions.

    As suggested in earlier post, I really dont have the permission to add any extra modules there on my company's Win 2003 Server. It was by pure luck that it happens to have DWIM Perl installed there. But I cannot add any modules there. Cannot even copy files. Hence cannot use the module you suggest

    The module you suggested runs on top of Net::SSH2, so if Net::SSH2 is not able to run the command, then any module that runs on top of that might have same issue.

    Perlpetually Indebted To PerlMonks

    Win 7 at Work. Living through it....Linux at home. Loving it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-12-25 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (163 votes), past polls