Commit a6f0680e authored by Mac Newbold's avatar Mac Newbold

Updated to function with stack. Disallows identical vlans from file, and

allows removal of multiple VLANs at one time.
parent 12a8fa73
...@@ -26,6 +26,7 @@ my $f = ""; #AutoVLAN filename ...@@ -26,6 +26,7 @@ my $f = ""; #AutoVLAN filename
my $l = 0; #List all VLANs on switch my $l = 0; #List all VLANs on switch
my $m = ""; #Create VLAN - name my $m = ""; #Create VLAN - name
my @vlan = (); #VLAN Members - list of MAC Addresses my @vlan = (); #VLAN Members - list of MAC Addresses
my @r = (); #VLAN(s) to be removed
my $b = 0; #Block (bool) (two-way switch) my $b = 0; #Block (bool) (two-way switch)
my $c = 1; #Confirm Changes (bool) (two-way switch) my $c = 1; #Confirm Changes (bool) (two-way switch)
my $v = 0; #Verbose (bool) !Must default quiet; switch only turns on! my $v = 0; #Verbose (bool) !Must default quiet; switch only turns on!
...@@ -38,7 +39,7 @@ my $all=0; ...@@ -38,7 +39,7 @@ my $all=0;
my @outlets=(); my @outlets=();
my %Interfaces=(); my %Interfaces=();
&ParseArgs(*ARGV,*i,*p,*d,*e,*a,*dup,*s,*f,*l,*m,*vlan,*b,*c,*v, &ParseArgs(*ARGV,*i,*p,*d,*e,*a,*dup,*s,*f,*l,*m,*vlan,*r,*b,*c,*v,
*on,*off,*cyc,*all,*outlets,*Interfaces); *on,*off,*cyc,*all,*outlets,*Interfaces);
use lib '/n/moab/x/newbold/ucd-snmp-4.1.1/perl/SNMP/blib/lib'; use lib '/n/moab/x/newbold/ucd-snmp-4.1.1/perl/SNMP/blib/lib';
...@@ -47,7 +48,7 @@ use SNMP; ...@@ -47,7 +48,7 @@ use SNMP;
#$SNMP::debugging = 1; #$SNMP::debugging = 1;
&SNMP::addMibDirs('/usr/local/share/snmp/mibs'); &SNMP::addMibDirs('/usr/local/share/snmp/mibs');
if (($i=~/155\.99\.214\.(.*)/) && ($1=~ /^170$|^171$/)) { if (($i=~/155\.99\.214\.17(.*)/) && ($1=~ /^0$|^1$|^2$|^3$/)) {
&SNMP::addMibFiles( &SNMP::addMibFiles(
# '/usr/local/share/snmp/mibs/RFC1155-SMI.txt', # '/usr/local/share/snmp/mibs/RFC1155-SMI.txt',
# '/usr/local/share/snmp/mibs/RFC1213-MIB.txt', # '/usr/local/share/snmp/mibs/RFC1213-MIB.txt',
...@@ -129,9 +130,12 @@ if ($f) { ...@@ -129,9 +130,12 @@ if ($f) {
&AutoVLANConfig(\$sess,\$f,\$v); &AutoVLANConfig(\$sess,\$f,\$v);
} }
if ($r) { if (@r) {
&GetToken(\$sess,\$v); &GetToken(\$sess,\$v);
&RemoveVLAN(\$sess,\$r,\$v); foreach $x (sort @r)
{
&RemoveVLAN(\$sess,\$x,\$v);
}
&ReleaseToken(\$sess,\$v); &ReleaseToken(\$sess,\$v);
} }
...@@ -182,10 +186,10 @@ if ($off || $on || $cyc) { ...@@ -182,10 +186,10 @@ if ($off || $on || $cyc) {
# SUB PARSEARGS---------------------- # SUB PARSEARGS----------------------
# #
sub ParseArgs { sub ParseArgs {
local(*CMDS,*i,*p,*d,*e,*a,*dup,*s,*f,*l,*m,*vlan,*b,*c,*v, local(*CMDS,*i,*p,*d,*e,*a,*dup,*s,*f,*l,*m,*vlan,*r,*b,*c,*v,
*on,*off,*cyc,*all,*outlets,*Interfaces) = @_; *on,*off,*cyc,*all,*outlets,*Interfaces) = @_;
my $help=0; my $help=0;
if (@CMDS < 2) {$help = 2; } if (@CMDS < 1) {$help = 2; }
while (@CMDS != 0 && $CMDS[0] =~ /^(-|\+)/) { while (@CMDS != 0 && $CMDS[0] =~ /^(-|\+)/) {
$_ = shift(@CMDS); $_ = shift(@CMDS);
#print "Item=$_*\n"; #print "Item=$_*\n";
...@@ -227,7 +231,12 @@ sub ParseArgs { ...@@ -227,7 +231,12 @@ sub ParseArgs {
push(@vlan, shift(@CMDS)); push(@vlan, shift(@CMDS));
} }
} }
elsif (/^-r(.*)/) {$r = ($1 ? $1 : shift(@CMDS));} elsif (/^-r(.*)/) {
push(@r, ($1 ? $1 : shift(@CMDS)));
while ( @CMDS>0 && ! ($CMDS[0] =~ /^(-|\+)(.*)/ ) ) {
push(@r, shift(@CMDS));
}
}
elsif (/^-v(.*)/) {$v = 1;} elsif (/^-v(.*)/) {$v = 1;}
elsif (/^-b(.*)/) {$b = 0;} elsif (/^-b(.*)/) {$b = 0;}
elsif (/^\+b(.*)/) {$b = 1;} elsif (/^\+b(.*)/) {$b = 1;}
...@@ -245,7 +254,7 @@ sub ParseArgs { ...@@ -245,7 +254,7 @@ sub ParseArgs {
"\t[-p<port> <port> <x>..<y> ... ]\n", "\t[-p<port> <port> <x>..<y> ... ]\n",
"\t[-d[isable]|-e[nable]] [+a|-a] [-s<speed>] [-dup<duplex>]\n", "\t[-d[isable]|-e[nable]] [+a|-a] [-s<speed>] [-dup<duplex>]\n",
"\t[-m<vlan name>] [[-vlan<MAC Addr.>] | [-f<filename>]]\n", "\t[-m<vlan name>] [[-vlan<MAC Addr.>] | [-f<filename>]]\n",
"\t[-r<vlan #>] [-l]\n", "\t[-r<vlan #> <vlan #> ... ] [-l]\n",
"\t[ [-on|-off|-cyc] [-all] | [-<n>] ]\n"; "\t[ [-on|-off|-cyc] [-all] | [-<n>] ]\n";
} }
...@@ -267,7 +276,7 @@ sub ParseArgs { ...@@ -267,7 +276,7 @@ sub ParseArgs {
" -l List all VLANs on switch (ell, not #1)\n", " -l List all VLANs on switch (ell, not #1)\n",
" -m Make a VLAN\n", " -m Make a VLAN\n",
" -vlan Add MAC Address to VLAN\n", " -vlan Add MAC Address to VLAN\n",
" -r Remove VLAN", " -r Remove VLAN(s)",
" -on Turn on outlets\n", " -on Turn on outlets\n",
" -off Turn off outlets\n", " -off Turn off outlets\n",
" -cyc Power Cycle outlets\n", " -cyc Power Cycle outlets\n",
...@@ -278,20 +287,20 @@ sub ParseArgs { ...@@ -278,20 +287,20 @@ sub ParseArgs {
die("\b\n") if ($help); die("\b\n") if ($help);
#Now die on any combinations that don't make sense... #Now die on any combinations that don't make sense...
die("What shall I do?\n") die("What shall I do?\n")
if (! ($d||$e||$a||$s||$dup||@vlan||$f||$r||$l||$on||$off||$cyc) ); if (! ($d||$e||$a||$s||$dup||@vlan||$f||@r||$l||$on||$off||$cyc) );
if (@vlan||$f||@r||$l) { $i = "Gamma"; }
if ($i =~ /Alpha/i) { $i = "155.99.214.170"; } if ($i =~ /Alpha/i) { $i = "155.99.214.170"; }
if ($i =~ /Beta/i) { $i = "155.99.214.171"; } if ($i =~ /Beta/i) { $i = "155.99.214.171"; }
if ($i =~ /Gamma/i) { $i = "155.99.214.172"; }
if ($i =~ /Delta/i) { $i = "155.99.214.173"; }
die("$i is not an Intel 510 Switch: Possible completions are\n", die("$i is not an Intel 510 Switch: Possible completions are\n",
"Alpha/155.99.214.170\n Beta/155.99.214.171\n") "Alpha/155.99.214.170\n Beta/155.99.214.171\n",
if ( (!(($i=~/155\.99\.214\.(.*)/) && ($1=~ /^170$|^171$/))) && "Gamma/155.99.214.172\n Delta/155.99.214.173\n")
($d || $e || $a || $s || $dup || @vlan || $f || $r || $l )); if ( (!(($i=~/155\.99\.214\.17(.*)/) && ($1=~ /^0$|^1$|^2$|^3$/))) &&
($d || $e || $a || $s || $dup || @vlan || $f || @r || $l ));
die("$i is not a power controller.\n") die("$i is not a power controller.\n")
if ( (($i=~/155\.99\.214\.(.*)/) && ($1=~ /^170$|^171$/)) && if ( (($i=~/155\.99\.214\.(.*)/) && ($1=~ /^0$|^1$|^2$|^3$/)) &&
($on||$off||$cyc) ); ($on||$off||$cyc) );
die("Can't remove a Control VLAN.\n")
if ( (($i=~/155\.99\.214\.170/) && ($r) && ($r==38)) ||
(($i=~/155\.99\.214\.171/) && ($r) && ($r==42)));
#(Control VLAN is 41 on 170, 42 on 171)
die("Can't enable and disable at the same time.\n") if ($d && $e); die("Can't enable and disable at the same time.\n") if ($d && $e);
die("Can't use auto with duplex or speed.\n") if (($a=~/en/)&&($dup||$s)); die("Can't use auto with duplex or speed.\n") if (($a=~/en/)&&($dup||$s));
my $n=0; my $n=0;
...@@ -397,6 +406,10 @@ sub ReleaseToken { ...@@ -397,6 +406,10 @@ sub ReleaseToken {
$sess->set([[$TokenRelease,0,"save","INTEGER"]]); $sess->set([[$TokenRelease,0,"save","INTEGER"]]);
$RetVal = $sess->get([[$TokenReleaseResult,0]]); $RetVal = $sess->get([[$TokenReleaseResult,0]]);
print "VLAN Configuration Save Result is $RetVal\n" if ($v); print "VLAN Configuration Save Result is $RetVal\n" if ($v);
while ($RetVal eq "notReady") {
$RetVal = $sess->get([[$TokenReleaseResult,0]]);
print "VLAN Configuration Save Result is $RetVal\n" if ($v);
}
if ($RetVal ne 'success') { if ($RetVal ne 'success') {
die("VLAN Reconfiguration Failed. No changes saved.\n"); die("VLAN Reconfiguration Failed. No changes saved.\n");
} }
...@@ -425,6 +438,7 @@ sub GetToken { ...@@ -425,6 +438,7 @@ sub GetToken {
print "VLAN Token Claim Result is $RetVal\n" if ($v); print "VLAN Token Claim Result is $RetVal\n" if ($v);
if ($RetVal ne 'success') { if ($RetVal ne 'success') {
my $Owner = $sess->get([[$TokenOwner,0]]); my $Owner = $sess->get([[$TokenOwner,0]]);
print "VLAN Token Claim Result is $RetVal\n";
die("Can't edit VLANs: Token taken by $Owner\n"); die("Can't edit VLANs: Token taken by $Owner\n");
} }
} }
...@@ -489,10 +503,10 @@ sub AutoVLANConfig { ...@@ -489,10 +503,10 @@ sub AutoVLANConfig {
sub ReadIRFile { sub ReadIRFile {
local(*f,*VLANS) = @_; local(*f,*VLANS) = @_;
my %exists = ();
my $skip = 1; my $skip = 1;
my $switch = $i; my $switch = $i;
my $section = ""; my $section = "";
my $go = 0;
if ( !open(IR,$f) ) { die("Couldn't open $f for VLAN Setup.\n"); } if ( !open(IR,$f) ) { die("Couldn't open $f for VLAN Setup.\n"); }
while (<IR>) { while (<IR>) {
chop; chop;
...@@ -500,17 +514,8 @@ sub ReadIRFile { ...@@ -500,17 +514,8 @@ sub ReadIRFile {
if ( /^start vlan/i ) { $skip = 0 ; next; } if ( /^start vlan/i ) { $skip = 0 ; next; }
if ($skip) { next; } if ($skip) { next; }
if ( /^end vlan/i ) { $skip = 1 ; next; } if ( /^end vlan/i ) { $skip = 1 ; next; }
if ( /^end/ ) { $go = 0; next; } if ( /^switch/i ) { next; }
if ( /^switch (.*)$/i) { if ( /^end/i ) { next; }
$section = $1;
if ($section=~/[a-z]/) {
if ($section =~ /Alpha/i) { $section = "155.99.214.170"; }
if ($section =~ /Beta/i) { $section = "155.99.214.171"; }
}
$go = ( $section eq $switch ? 1 : 0 );
next;
}
if (! $go) { next; }
# If I'm in the right section, turn the line into a VLAN # If I'm in the right section, turn the line into a VLAN
my @v= split; my @v= split;
my $n=0; my $n=0;
...@@ -526,7 +531,11 @@ sub ReadIRFile { ...@@ -526,7 +531,11 @@ sub ReadIRFile {
if (!(($v[$n]=~/^([a-f]|\d)*$/i)&&(length ($v[$n])== 12))); if (!(($v[$n]=~/^([a-f]|\d)*$/i)&&(length ($v[$n])== 12)));
$n++; $n++;
} }
push(@VLANS, \@v); @v = sort @v;
if (!defined $exists{ join(' ',@v) }) {
push(@VLANS, \@v);
$exists{join(' ',@v)} = 1;
}
} }
close (IR); close (IR);
} }
...@@ -623,11 +632,14 @@ sub RemoveVLAN(*sess,*r,*v) { ...@@ -623,11 +632,14 @@ sub RemoveVLAN(*sess,*r,*v) {
my $DeleteOID = ".1.3.6.1.4.1.343.6.11.1.9.1.4"; my $DeleteOID = ".1.3.6.1.4.1.343.6.11.1.9.1.4";
my $RetVal = "Undef."; my $RetVal = "Undef.";
print " Removing VLAN #$r ... "; print " Removing VLAN #$r ... ";
$RetVal = $sess->set([[$DeleteOID,$r,"delete","INTEGER"]]); if ($r == 25) {
print "",($RetVal? "Succeeded":"Failed"),".\n"; print "VLAN #$r is the Control VLAN, and cannot be removed.\n";
if (! defined ($RetVal) ) { } else {
&ReleaseToken(*sess,*v); $RetVal = $sess->set([[$DeleteOID,$r,"delete","INTEGER"]]);
die("VLAN #$r does not exist on this switch.\n"); print "",($RetVal? "Succeeded":"Failed"),".\n";
if (! defined ($RetVal) ) {
print "VLAN #$r does not exist on this switch.\n";
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment