I have a script that cycles through several systems and collects the data of various shell commands. I do not know which systems will be up or have a SSH server running. Thus I need the script to skip any bad connections.
#!/usr/bin/perl use strict; use warnings; use Net::SSH::Perl; my ($ssh, $cmd, $x, $stdout, $stderr, $exit); # commands to be passed to remote server my @commands = ( 'uptime', 'tail /var/log/secure', 'tail /var/log/messages', 'exit'); my $user = 'root'; my $pass = 'root1'; open LOGFILE, "> hw-survey-log" || die "could not open file $!"; # connect to many hosts, on after another for ($x=3; $x<255; $x+=2){ # make new connection $ssh = Net::SSH::Perl->new("10.0.0.$x", protocol => 2) || next; $ssh->login($user, $pass) || next; print LOGFILE "\nHostname: 10.0.0.$x:\n"; # execute all commands foreach $cmd (@commands){ # pass command and record results ($stdout, $stderr, $exit) = ssh->cmd($cmd); print LOGFILE $stdout.$stderr.$exit; } } close LOGFILE;
I thought that the || next; would handle this. However, the script dies after the first attempt:
Can't connect to 10.0.0.3, port 22: No route to host at /usr/lib/perl5 +/vendor_perl/5.8.5/Net/SSH/Perl.pm line 206.
I can see in the pm code that there is a die:
connect($sock, sockaddr_in($rport, $raddr)) or die "Can't connect to $ssh->{host}, port $rport: $!";
It would be nice if there was a way around this without modifying the module.

Neil Watson
watson-wilson.ca


In reply to Net::SSH::Perl and handling dead hosts by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.