Monks,

I have a script that does what I want, but I was wondering if there is a cleaner way of writing this. I thought about putting it in a for(each) loop, but if the run subroutine is successfull, I don't want it to process any of the other ip's. I haven't figured a way to prevent this using a for loop. Anyway, I would appreciate any advice.
Also, you might wonder what 256 is. That is the error code that is returned if ssh fails.
#!/usr/bin/perl -w # Attempt to connect to the server on each interface until successfull use strict; use Net::SSH qw(ssh); my @host = qw[ 192.168.15.2 192.168.16.2 192.168.17.2 192.168.18.2 ]; my $stout; $stout = run($host[0]); if ($stout == '256'){ print "$host[0] not responding, trying $host[1]\n"; $stout = run($host[1]); if ($stout == '256'){ print "$host[1] not responding, trying $host[2]\n"; $stout = run($host[2]); if ($stout == '256'){ print "$host[2] not responding, trying $host[3]\n"; $stout = run($host[3]); } } } else{ exit; } sub run { my $host = $_[0]; my $user = 'root'; my $cmd = '/usr/local/scripts/temp.sh'; ssh("$user\@$host", $cmd); }
Thanks,

Dru

In reply to Not Using So Many 'if' Statements by Dru

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.