#!/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 the 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 { ... }