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