samuelk1 has asked for the wisdom of the Perl Monks concerning the following question:
I've written a perl script that connects to a bunch of crusty old Cisco 877 routers and backs up their configuration over the WAN.
The script is below. The first part just parses the nagios status.dat file, which has the current public IP address of each router. Some of them have dynamic IP addresses and these are kept up to date through some passive checks in Nagios.
The firmware on these routers does not handle SSH particularly well. Nagios is checking that the routers can be accessed using SSH over the WAN every 5 minutes. If you try and SSH into the router around 5-10 seconds after this check place, the connection is reset because the router is still tearing down the SSH session. The workaround in the script is to just try and connect in 5 seconds using a goto. I was wondering if there is a good way to rewrite this loop without goto? Or is goto OK in this case? I have had one idea, but the problem with it is it still waits 5 seconds even after the last retry has taken place before checking the next router:use warnings; use strict; open STATUS_DAT, "</usr/local/nagios4/var/status.dat"; my $hs = 0; my $r = 0; my $r_ip; my $r_name; my $NAME = 0; my $IP = 1; my @routers; sub write_config { my $config = shift; my $filename = shift; open CONFIG, ">$filename"; print CONFIG $config; print "Wrote $filename\n"; } while (<STATUS_DAT>) { $hs = 1, next if (/hoststatus/); $hs = 0, $r = 0, next if ($hs and /}/); next if (not $hs); $r = 1, $r_name = $+, next if (/host_name=(.*ROU)\n/); next if (not $r); $r_ip = $+, push @routers, [$r_name, $r_ip] if (/_PUBLICIP=[01];(. +*)\n/); } for my $router (@routers) { my $retries = 0; retry: my $output = `expect -f show_running_config.exp $router->[$IP]`; if ($? == 0) { write_config($output, "$router->[$NAME].cfg"); } else { if ($retries <= 3) { sleep 5; $retries++; goto retry; } else { print "Failed to get config for $router->[$NAME] at $route +r->[$IP]: $output\n"; } } }
Thanks in advance for any advice. Regards, Samfor my $router (@routers) { my $retries = 0; while ($retries <= 3) { my $output = `expect -f show_running_config.exp $router->[$IP] +`; write_config($output, "$router->[$NAME].cfg"), last if ($? == +0); } continue { sleep 5; $retries++; } if ($retries > 3) { print "Failed to get config for $router->[$NAME] at $router->[ +$IP]: $output\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Wait and retry loop without goto
by aaron_baugher (Curate) on May 15, 2015 at 02:50 UTC | |
by samuelk1 (Initiate) on May 15, 2015 at 03:13 UTC | |
|
Re: Wait and retry loop without goto
by einhverfr (Friar) on May 15, 2015 at 06:20 UTC | |
by Laurent_R (Canon) on May 15, 2015 at 06:30 UTC | |
by einhverfr (Friar) on May 15, 2015 at 07:39 UTC | |
by Laurent_R (Canon) on May 15, 2015 at 12:19 UTC | |
|
Re: Wait and retry loop without goto
by jellisii2 (Hermit) on May 15, 2015 at 12:01 UTC |