X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=clients%2Fciscong.pl;h=9d2dac21ca4a2ed024f42eb98895dd51dc57481d;hb=6dc95ee700d271c420464837f68f7d2a3c255dd4;hp=80d67ddc1d8918f6ec81ed7f98c851eadda5096c;hpb=a78ca3c9628b209500205ad77b333bd656175f34;p=nms diff --git a/clients/ciscong.pl b/clients/ciscong.pl index 80d67dd..9d2dac2 100644 --- a/clients/ciscong.pl +++ b/clients/ciscong.pl @@ -12,6 +12,13 @@ use Net::Ping; use Data::Dumper; +#my $patchlist = "/root/patchlist.txt"; +#my $switches = "/root/switches.txt"; +#my $patchlist = "/home/eirikn/patchlist.txt.eirik"; +#my $switches = "/home/eirikn/switches.txt.eirik"; +my $patchlist = "/root/patchlist.txt.d01-1-2"; +my $switches = "/root/switches.txt.d01-1-2"; + BEGIN { require "../include/config.pm"; @@ -28,17 +35,43 @@ sub ios_getroute { return 1; } -my $ios_server = "noc-gw.net.tg07.gathering.org"; -my $vlannumber = 16; +#my $ios_server = "noc-gw.net.tg07.gathering.org"; +#my $vlannumber = 16; #my $ios = nms::ios_connect($ios_server, $nms::config::ios_user, $nms::config::ios_pass) # or die "Unable to connect to cisco"; -my $ios = Net::Telnet::Cisco->new(Host => $ios_server, - Errmode => 'return', - Prompt => '/[^\s\(]+(\([^\(]\)){0,1}[#>]/'); -$ios->login($nms::config::ios_user, $nms::config::ios_pass); -$ios->enable; + +sub start_vlan { + my ($ios, $vlan) = @_; + + $ios->cmd("conf t"); + $ios->cmd("int vlan $vlan"); + $ios->cmd("ip add 192.168.1.254 255.255.255.0 secondary"); + $ios->cmd("exit"); + $ios->cmd("exit"); +} + +sub stop_vlan { + my ($ios, $vlan) = @_; + + $ios->cmd("conf t"); + $ios->cmd("int vlan $vlan"); + $ios->cmd("no ip add 192.168.1.254 255.255.255.0 secondary"); + $ios->cmd("exit"); + $ios->cmd("exit"); +} + + + +sub do_distro { + my ($dip, $newip, $vlan, $switchname) = @_; + + my $ios = Net::Telnet::Cisco->new(Host => $dip, + Errmode => 'return', + Prompt => '/[^\s\(]+(\([^\(]\)){0,1}[#>]/'); + $ios->login($nms::config::ios_user, $nms::config::ios_pass); + $ios->enable; #nms::ios_enable($ios); #$ios->cmd(); @@ -48,46 +81,134 @@ $ios->enable; #nms::ios_close($ios); # Disable paging -$ios->cmd("terminal length 0"); + $ios->cmd("terminal length 0"); #my @routes = $ios->cmd("show ip route"); -die "Already routed up 192.168.1.0/24" if (ios_getroute($ios, "192.168.1.0") == 1); + if (ios_getroute($ios, "192.168.1.0") == 1) { + print "Already routed up 192.168.1.0/24\n" ; + return; + } + my $zyxeloldip = "192.168.1.1"; + start_vlan($ios, $vlan); +### Do things +## ZyxelNG connect -$ios->cmd("conf t"); -$ios->cmd("int vlan $vlannumber"); -$ios->cmd("ip add 192.168.1.254 255.255.255.0 secondary"); -$ios->cmd("exit"); -$ios->cmd("exit"); + my $p = Net::Ping->new(); + printf "Waiting for zyxel to come up...\n"; + my $counter = 0; + while (1) { + if ($counter > 180) { + print "Waiting for zyxel on $dip:$vlan timed out, wanted to set ip: $newip"; + stop_vlan($ios, $vlan); + return; + } + last if $p->ping($zyxeloldip); + print "pinging...\n"; + sleep 1; + $counter++; + } + print "Zyxel is alive..\n"; + $p->close(); + + system("perl ./zyxelng.pl 192.168.1.1 $newip $switchname"); -my $zyxeloldip = "192.168.1.1"; +#### -### Do things -## ZyxelNG connect + stop_vlan($ios, $vlan); -my $p = Net::Ping->new(); -printf "Waiting for zyxel to come up...\n"; -while (1) { - last if $p->ping($zyxeloldip); - print "pinging...\n"; - sleep 1; + $ios->close(); + } -print "Zyxel is alive..\n"; -$p->close(); -system("perl ./zyxelng.pl 192.168.1.1 192.168.2.1"); +## Collect switch ips + +my %switchips; + +open(SWITCHES, $switches) or die "Unable to open switches"; +while() { + my ($ip, $net, $name) = split; + + if ($name =~ /e\d+-\d/) { + die "We only support /26 nets for now you wanted $net" if ($net ne "26"); + $switchips{$name} = $ip; + } +} +close(SWITCHES); + +sub switch_info { + my ($switch, $distro, $port) = @_; + + $switch =~ /e(\d+)-(\d)/; + my ($row, $place) = ($1, $2); + my $ipnet = $switchips{$switch}; + my $vlan = $row . $place; + my ($first, $second, $third, $fourth) = split(/\./, $ipnet); + my $ip = "$first.$second.$third.".(int($fourth)+2); + my $dip = $distro.".net.tg07.gathering.org"; + + return ($row, $place, $ipnet, $vlan, $ip, $dip); +} + +sub first_run { + + open(PATCHLIST, $patchlist) or die "Unable to open patchlist"; + while () { + my ($switch, $distro, $port) = split; + + print "First run...\n"; + my ($row, $place, $ipnet, $vlan, $ip, $dip) = switch_info($switch, $distro, $port); + print "Switch: $switch, Distro: $distro, vlan: $vlan\n"; + print "Ip net: $ipnet\n"; + print "Ip: $ip\n"; + + do_distro($dip, $ip, $vlan, $switch); +# my ($dip, $newip, $vlan) = @_; + } + close(PATCHLIST); +} + +sub verify_run { + open(PATCHLIST, $patchlist) or die "Unable to open patchlist"; + while () { + print "Verify run....\n"; + my ($switch, $distro, $port) = split; + + my ($row, $place, $ipnet, $vlan, $ip, $dip) = switch_info($switch, $distro, $port); + print "Switch: $switch, Distro: $distro, vlan: $vlan\n"; + print "Ip net: $ipnet\n"; + print "Ip: $ip\n"; + + my $p = Net::Ping->new(); + printf "Checking if zyxel is up $dip:$vlan $ip...\n"; + my $counter = 0; + while (1) { + if ($counter > 10) { + print "No answer from $dip:$vlan $ip, trying to route it up\n"; + do_distro($dip, $ip, $vlan, $switch); + last; + } + last if $p->ping($ip); + print "pinging...\n"; + sleep 1; + $counter++; + } + +# my ($dip, $newip, $vlan) = @_; + } + close(PATCHLIST); +} -#### -$ios->cmd("conf t"); -$ios->cmd("int vlan $vlannumber"); -$ios->cmd("no ip add 192.168.1.254 255.255.255.0 secondary"); -$ios->cmd("exit"); -$ios->cmd("exit"); +if ($#ARGV > -1) { + first_run(); +} +else { + verify_run(); +} -$ios->close();