Morning Monks,

I have the following script which worked in my testing, but of course when I try to put it into production, it failed. The problem is, the script executes the failover sub before the run subroutine has completed resulting in ip conflicts. Can someone take a look at my code and let me know how I can prevent the failover sub from executing until the run sub is complete.

Thanks,
Dru
#!/usr/bin/perl -w # This script tests to see if the primary fw is up. If it is not, then + it will attempt to connect via ssh to it and bring it down and make +itself the primary. use strict; use Mail::Sender; use IO::Socket::INET; use Net::SSH qw(ssh); my @host = qw[ 192.168.9.2 192.168.1.2 192.168.2.2 192.168.6.2 ]; my $port = '256'; # Check to see if all interfaces are listening on the fw port. If not, + run the connect sub. foreach (@host) { check($_, $port); } sub check { my ($ip, $port) = @_; my $sock = IO::Socket::INET->new( PeerAddr => $ip, PeerPort => $port, Proto => 'tcp', Timeout => 5, ); if ($sock) { print "$ip is listening on port $port\n"; return 1; } else{ print "$ip is NOT listening on port $port proceeding to failover. +\n" and &connect(); return; } } my $stout; my $success; # Attempt to connect to any of the fw's interfaces and then execute th +e run subroutine sub connect { for(@host){ $stout = run($_); unless($stout == 256) { $success++; last; } print "$_ not responding, trying next host\n"; } print "All hosts not responding" unless $success; } # Use Net::SSH to connect to the primary firewall and execute the down +_pri script to bring it's interfaces down. sub run { my $host = $_[0]; my $user = 'root'; my $cmd = '/usr/local/scripts/down_pri.pl'; ssh("$user\@$host", $cmd); if($stout == 256) { print "Problem sshing: $!\n" and exit; } else{ &failover(); } } # Run local commands on this server sub failover{ ... my @cmds = ($cmd1, $cmd2, $cmd3, $cmd4, $cmd5, $cmd6, $cmd7, $cmd8, $ +cmd9, $cmd10, $cmd11); foreach (@cmds){ system($_); } mail(); } # This sub uses the Mail::Sender module to email to our pagers sub mail { ... }

In reply to Waiting for one Sub to Exit First 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.