From c1d4c44042edbcdfe0b998360460fe0f20902274 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Wed, 28 Jan 2009 08:42:18 -0800 Subject: bgp: cleanup Turn on strict, and declare variables. Simplify code where possible. --- scripts/bgp/vyatta-bgp.pl | 88 +++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 56 deletions(-) (limited to 'scripts/bgp/vyatta-bgp.pl') diff --git a/scripts/bgp/vyatta-bgp.pl b/scripts/bgp/vyatta-bgp.pl index 6ca3480e..931069c2 100755 --- a/scripts/bgp/vyatta-bgp.pl +++ b/scripts/bgp/vyatta-bgp.pl @@ -1,46 +1,35 @@ #!/usr/bin/perl +use strict; use lib "/opt/vyatta/share/perl5/"; use Vyatta::Config; use Vyatta::Misc; use Getopt::Long; -GetOptions("check-peer-name=s" => \$peername, - "check-as" => \$checkas, - "check-peer-groups" => \$checkpeergroups, - "check-if-peer-group" => \$checkifpeergroup, - "peergroup=s" => \$pg, - "as=s" => \$as, - "neighbor=s" => \$neighbor, +my $pg = -1; +my ($as, $neighbor); + +GetOptions( + "peergroup=s" => \$pg, + "as=s" => \$as, + "neighbor=s" => \$neighbor, + "check-peer-name=s" => sub { check_peer_name( $_[1] ) }, + "check-as" => sub { check_as($pg, $neighbor, $as); }, + "check-peer-groups" => sub { check_for_peer_groups($pg, $as); }, + "check-if-peer-group" => sub { check_if_peer_group($pg); }, ); -if (defined $peername) { check_peer_name($peername); } -elsif (defined $checkpeergroups && - defined $pg && - defined $as) { check_for_peer_groups($pg, $as); } -elsif (defined $neighbor && - defined $as && - defined $checkas && - defined $pg) { check_as($pg, $neighbor, $as); } -elsif (defined $neighbor && - defined $as && - defined $checkas) { check_as(-1, $neighbor, $as); } -elsif (defined $pg && - defined $checkifpeergroup) { check_if_peer_group($pg); } - - exit 0; sub check_if_peer_group { my $neighbor = shift; - - my $version = is_ip_v4_or_v6($neighbor); - exit 1 if defined $version; + + exit 1 if is_ip_v4_or_v6($neighbor); exit 0; } # Make sure the neighbor is a proper IP or name -sub check_peer_name() { +sub check_peer_name { my $neighbor = shift; $_ = $neighbor; @@ -60,22 +49,20 @@ sub check_peer_name() { # Make sure we aren't deleteing a peer-group that has # neighbors configured to us it -sub check_for_peer_groups() { +sub check_for_peer_groups { my $config = new Vyatta::Config; my $pg = shift; my $as = shift; - my $node = $pg; - my @peers, @neighbors; + my @peers; # short circuit if the neighbor is an IP rather than name - my $version = is_ip_v4_or_v6($node); - return if defined $version; + return if is_ip_v4_or_v6($pg); # get the list of neighbors and see if they have a peer-group set $config->setLevel("protocols bgp $as neighbor"); my @neighbors = $config->listNodes(); - foreach $node (@neighbors) { + foreach my $node (@neighbors) { my $peergroup = $config->returnValue("$node peer-group"); if ($peergroup eq $pg) { push @peers, $node; } } @@ -83,7 +70,7 @@ sub check_for_peer_groups() { # if we found peers in the previous statements # notify an return errors if (@peers) { - foreach $node (@peers) { + foreach my $node (@peers) { print "neighbor $node uses peer-group $pg\n"; } @@ -96,7 +83,7 @@ sub check_for_peer_groups() { # make sure nodes are either in a peer group of have # a remote AS assigned to them. -sub check_as() { +sub check_as { my $pg = shift; my $neighbor = shift; my $as = shift; @@ -104,32 +91,21 @@ sub check_as() { my $pgtest = $neighbor; # if this is peer-group then short circuit this - my $version = is_ip_v4_or_v6($node); - return if ! defined $version; + return unless is_ip_v4_or_v6($pg); $config->setLevel("protocols bgp $as neighbor $neighbor"); - $remoteas = $config->returnValue("remote-as"); + my $remoteas = $config->returnValue("remote-as"); - if (! defined $remoteas) { - if ($pg > 0) { - $peergroup = 1; - $peergroupas = 1; - } - else { - $peergroup = $config->returnValue("peer-group"); - $peergroupas = $config->returnValue(" .. $peergroup remote-as"); - } + return unless $remoteas; - if (! defined $peergroup) { - print "protocols bgp $as neighbor $neighbor: you must define a remote-as or peer-group\n"; - exit 1; - } + return if ($pg > 0); - if (! defined $peergroupas) { - print "protocols bgp $as neighbor $neighbor: you must define a remote-as in this neighbor or in peer-group $peergroup\n"; - exit 1; - } - } + my $peergroup = $config->returnValue("peer-group"); + my $peergroupas = $config->returnValue(" .. $peergroup remote-as"); - return; + die "protocols bgp $as neighbor $neighbor: must define a remote-as or peer-group\n" + unless $peergroup; + + die "protocols bgp $as neighbor $neighbor: must define a remote-as in neighbor or peer-group $peergroup\n" + unless $peergroupas; } -- cgit v1.2.3