vlansync.in 2.65 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
2 3 4

#
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24 25
#

26 27 28 29 30 31
#
# vlansync - Synchronize the switch vlans to what's in the vlans table
#            by removing extra vlans/ports, and adding missing ones
#

my $TBROOT = "@prefix@";
32 33

use English;
34 35 36 37 38

$ENV{'PATH'} = '@prefix@/sbin:@prefix@/bin:/bin';

my $debug = 0;

39 40 41 42 43 44 45 46 47
my @extras = ();
my %missing = ();
my $force = 0;
my $args = ":".join(":",@ARGV).":";

if ($args =~ /:-h/) {
  die("Usage:\nvlansync [-h[elp]] [-f[orce]]\n".
      "vlansync will add any missing VLANs from the database to the switch.\n".
      "The -f[orce] option will delete any extra vlans from the switch.\n");
48 49
}

Mac Newbold's avatar
Mac Newbold committed
50
if ($args =~ /:-f/) { $force = 1; }
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

open(DIFF,"vlandiff |");

my $table = 0;
my $switch = 0;

sub num { $a <=> $b } # for sorting numerically...

while (<DIFF>) {
  chomp;
  if ( /table/ ) { $table = 1; $switch = 0; next; }
  if ( /switch/ ) { $table = 0; $switch = 1; next; }
  if ( $table ) {
    my ($id, $memb) = split("\t");
    $missing{$id} = $memb;
    print "Missing $id: $memb\n" if $debug > 1;
  }
  if ( $switch) {
    if ( /^\#(\d+): /) {
      push(@extras, $1);
      print "Extra: $1\n" if $debug > 1;
    } else { die("Couldn't find VLAN number in '$_'\n"); }
73 74 75
  }
}

76
@extras = sort num @extras;
77

78 79 80 81
if ($debug) {
  print "Found missing:\n";
  foreach $id (sort num keys %missing) {
    print "$id: $missing{$id}\n";
82
  }
83
  print "Found extras: @extras\n";
84 85
}

Mac Newbold's avatar
Mac Newbold committed
86
if ($force && @extras > 0 ) {
87 88 89
  print "Removing extra VLANs: @extras\n";
  system("snmpit -r @extras");
}
90

91
if ( (scalar keys %missing) == 0) { exit(0); }
92

93 94 95 96 97 98 99
print "Creating missing vlans:\n";
my $tmpfile = "/tmp/vlansync-$PID";
open(TMP,">$tmpfile") || die("Couldn't open $tmpfile for writing: $!\n");
print TMP "start vlan\n";
foreach $id (sort num keys %missing) {
  print "$id $missing{$id}\n";
  print TMP "$id $missing{$id}\n";
100
}
101 102
print TMP "end vlan\n";
close TMP;
103

104 105 106
system("snmpit -f $tmpfile");

system("rm -f $tmpfile");
107

108
exit(0);