resetvlans.in 2.71 KB
Newer Older
1 2
#!/usr/bin/perl

Leigh B. Stoller's avatar
Leigh B. Stoller committed
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 B. Stoller's avatar
Leigh B. Stoller committed
24 25 26
#


27 28 29
# This program takes a list of machines and removes all the VLANs
# whose every member is an argument.

30 31 32
#debug
my $d = 0;

33 34 35 36 37 38
#
# Configure variables
#
my $TB	= "@prefix@";

$snmpit = "$TB/bin/snmpit";
39 40

if ($#ARGV < 0) {
41
  die "Syntax: $0 <node> [<node>] ...\n";
42 43 44
}
open(SNMPIT,"$snmpit -l |") || die "Error running $snmpit -l\n";

45
%machines = ();
46 47

while ($#ARGV >= 0) {
48 49 50
  $node = pop(@ARGV);
  print "Found node $node\n" if $d;
  $machines{$node} = "";
51 52
}

53 54
print "Resetting VLANs for: ",join(" ",keys %machines),"\n";

55 56 57
@toremove = ();

while (<SNMPIT>) {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  chop;
  @line = split;
  $id = $line[0];
  if (! ($id =~ /[0-9]+/)) {
    next;
  }
  if ($id == 1) {
    next;
  }
  # Skip VLANs with no members, which tries to delete VLAN 1,
  # as well as private VLANs
  if (@line < 2) {
    next;
  }
  $remove = 1;
  foreach $member (@line[2..$#line]) {
    @elements = split(":",$member);
    if (! defined($machines{$elements[0]})) {
      $remove = 0;
      last;
78
    }
79 80 81 82
  }
  if ($remove == 1) {
    push(@toremove,$id);
  }
83 84
}

85 86 87 88
close(SNMPIT);

$vlans = join(" ",@toremove);
print "Removing VLANs: $vlans\n";
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
my @success=();
my @fail=();
my $error = 0;

if (@toremove > 0) {
  #system("$snmpit -u -r $toremove");
  open(RM,"$snmpit -r $vlans 2>&1 |") ||
    die "Error running $snmpit -r $toremove\n";
  while(<RM>) {
    print "snmpit:$_" if $d;
    chop;
    if ( /^[ \t]*$/ ) { next; }
    if ( /Removing VLAN \#(\d+) ... (.*)/ ) {
      my $num = $1;
      my $result = $2;
      if ( $result =~ /Succeeded/) {
	push(@success,$num);
      } else {
	print "$_\n";
	if (<RM>) { print "$_\n"; }
	push(@fail,$num);
	$error++;
      }
    } else {
      print "$_\n";
      $error++;
    }
  }
  close(RM);
119 120
}

121 122 123 124 125 126 127
if ($error || @fail ) {
  print "VLAN removal had $error error(s).\n";
  print "Removal failed for VLANs: @fail\n";
  print "Removal succeeded for VLANs: @success\n";
} else {
  print "Removal succeeded for VLANs: @success\n";
}
128

129
$error;