]> git.sesse.net Git - nms/blob - clients/ciscong.pl
Update
[nms] / clients / ciscong.pl
1 #!/usr/bin/perl
2 #
3 #
4
5 use warnings;
6 use strict;
7
8 use lib '../include';
9
10 use Net::Telnet::Cisco;
11 use Net::Ping;
12
13 use Data::Dumper;
14
15 #my $patchlist = "/root/patchlist.txt";
16 #my $switches = "/root/switches.txt";
17 #my $patchlist = "/home/eirikn/patchlist.txt.eirik";
18 #my $switches = "/home/eirikn/switches.txt.eirik";
19 my $patchlist = "/root/patchlist.txt.d01-1-2";
20 my $switches = "/root/switches.txt.d01-1-2";
21
22
23 BEGIN {
24         require "../include/config.pm";
25         eval {
26                 require "../include/config.local.pm";
27         };
28 }
29
30 sub ios_getroute {
31         my ($t, $net) = @_;
32
33         $t->cmd("show ip route".($net ? " $net" : '')) or return 0;;
34         
35         return 1;
36 }
37
38 #my $ios_server = "noc-gw.net.tg07.gathering.org";
39 #my $vlannumber = 16;
40
41 #my $ios = nms::ios_connect($ios_server, $nms::config::ios_user, $nms::config::ios_pass)
42 #       or die "Unable to connect to cisco";
43
44
45 sub start_vlan {
46         my ($ios, $vlan) = @_;
47
48         $ios->cmd("conf t");
49         $ios->cmd("int vlan $vlan");
50         $ios->cmd("ip add 192.168.1.254 255.255.255.0 secondary");
51         $ios->cmd("exit");
52         $ios->cmd("exit");
53 }
54
55 sub stop_vlan {
56         my ($ios, $vlan) = @_;
57
58         $ios->cmd("conf t");
59         $ios->cmd("int vlan $vlan");
60         $ios->cmd("no ip add 192.168.1.254 255.255.255.0 secondary");
61         $ios->cmd("exit");
62         $ios->cmd("exit");
63 }
64
65
66
67 sub do_distro {
68         my ($dip, $newip, $vlan, $switchname) = @_;
69
70         my $ios = Net::Telnet::Cisco->new(Host => $dip,
71                         Errmode => 'return',
72                         Prompt => '/[^\s\(]+(\([^\(]\)){0,1}[#>]/');
73         $ios->login($nms::config::ios_user, $nms::config::ios_pass);
74         $ios->enable;
75
76 #nms::ios_enable($ios);
77 #$ios->cmd();
78 #nms::ios_getroute($ios, "192.168.1.0");
79 #$ios->cmd("");
80
81 #nms::ios_close($ios);
82
83 # Disable paging
84         $ios->cmd("terminal length 0");
85
86 #my @routes = $ios->cmd("show ip route");
87
88         if (ios_getroute($ios, "192.168.1.0") == 1) {
89                 print "Already routed up 192.168.1.0/24\n" ;
90                 return;
91         }
92
93         my $zyxeloldip = "192.168.1.1";
94         start_vlan($ios, $vlan);
95
96 ### Do things
97 ## ZyxelNG connect
98
99         my $p = Net::Ping->new();
100         printf "Waiting for zyxel to come up...\n";
101         my $counter = 0;
102         while (1) {
103                 if ($counter > 180) {
104                         print "Waiting for zyxel on $dip:$vlan timed out, wanted to set ip: $newip";
105                         stop_vlan($ios, $vlan);
106                         return;
107                 }
108                 last if $p->ping($zyxeloldip);
109                 print "pinging...\n";
110                 sleep 1;
111                 $counter++;
112         }
113         print "Zyxel is alive..\n";
114         $p->close();
115
116         system("perl ./zyxelng.pl 192.168.1.1 $newip $switchname");
117
118 ####
119
120         stop_vlan($ios, $vlan);
121
122
123         $ios->close();
124
125 }
126
127 ## Collect switch ips
128
129 my %switchips;
130
131 open(SWITCHES, $switches) or die "Unable to open switches";
132 while(<SWITCHES>) {
133         my ($ip, $net, $name) = split;
134
135         if ($name =~ /e\d+-\d/) {
136                 die "We only support /26 nets for now you wanted $net" if ($net ne "26");
137                 $switchips{$name} = $ip;
138         }
139 }
140 close(SWITCHES);
141
142 sub switch_info {
143         my ($switch, $distro, $port) = @_;
144
145         $switch =~ /e(\d+)-(\d)/;
146         my ($row, $place) = ($1, $2);
147         my $ipnet = $switchips{$switch};
148         my $vlan = $row . $place;
149         my ($first, $second, $third, $fourth) = split(/\./, $ipnet);
150         my $ip = "$first.$second.$third.".(int($fourth)+2);
151         my $dip = $distro.".net.tg07.gathering.org";
152
153         return ($row, $place, $ipnet, $vlan, $ip, $dip);
154 }
155
156 sub first_run {
157
158         open(PATCHLIST, $patchlist) or die "Unable to open patchlist";
159         while (<PATCHLIST>) {
160                 my ($switch, $distro, $port) = split;
161                 
162                 print "First run...\n";
163                 my ($row, $place, $ipnet, $vlan, $ip, $dip) = switch_info($switch, $distro, $port);
164                 print "Switch: $switch, Distro: $distro, vlan: $vlan\n";
165                 print "Ip net: $ipnet\n";
166                 print "Ip: $ip\n";
167
168                 do_distro($dip, $ip, $vlan, $switch);
169 #       my ($dip, $newip, $vlan) = @_;
170         }
171         close(PATCHLIST);
172 }
173
174 sub verify_run {
175         open(PATCHLIST, $patchlist) or die "Unable to open patchlist";
176         while (<PATCHLIST>) {
177                 print "Verify run....\n";
178                 my ($switch, $distro, $port) = split;
179                 
180                 my ($row, $place, $ipnet, $vlan, $ip, $dip) = switch_info($switch, $distro, $port);
181                 print "Switch: $switch, Distro: $distro, vlan: $vlan\n";
182                 print "Ip net: $ipnet\n";
183                 print "Ip: $ip\n";
184
185                 my $p = Net::Ping->new();
186                 printf "Checking if zyxel is up $dip:$vlan $ip...\n";
187                 my $counter = 0;
188                 while (1) {
189                         if ($counter > 10) {
190                                 print "No answer from $dip:$vlan $ip, trying to route it up\n";
191                                 do_distro($dip, $ip, $vlan, $switch);
192                                 last;
193                         }
194                         last if $p->ping($ip);
195                         print "pinging...\n";
196                         sleep 1;
197                         $counter++;
198                 }
199
200 #       my ($dip, $newip, $vlan) = @_;
201         }
202         close(PATCHLIST);
203 }
204
205
206 if ($#ARGV > -1) {
207         first_run();
208 }
209 else {
210         verify_run();
211 }
212
213
214