]> git.sesse.net Git - nms/blob - clients/ciscong.pl
Commit working 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;
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;
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;
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 }
134
135 ## Collect switch ips
136
137 my %switchips;
138
139 open(SWITCHES, $switches) or die "Unable to open switches";
140 while(<SWITCHES>) {
141         my ($ip, $net, $name) = split;
142
143         if ($name =~ /e\d+-\d/) {
144                 die "We only support /26 nets for now you wanted $net" if ($net ne "26");
145                 $switchips{$name} = $ip;
146         }
147 }
148 close(SWITCHES);
149
150 sub switch_info {
151         my ($switch, $distro, $port) = @_;
152
153         $switch =~ /e(\d+)-(\d)/;
154         my ($row, $place) = ($1, $2);
155         my $ipnet = $switchips{$switch};
156         my $vlan = $row . $place;
157         my ($first, $second, $third, $fourth) = split(/\./, $ipnet);
158         my $ip = "$first.$second.$third.".(int($fourth)+2);
159         my $dip = $distro.".net.tg07.gathering.org";
160
161         return ($row, $place, $ipnet, $vlan, $ip, $dip);
162 }
163
164 sub first_run {
165
166         open(PATCHLIST, $patchlist) or die "Unable to open patchlist";
167         while (<PATCHLIST>) {
168                 my ($switch, $distro, $port) = split;
169
170                 #print "Testing: ".$ARGV[1]." $switch\n";
171                 next if (defined($ARGV[1]) and $ARGV[1] ne $switch);
172                 
173                 print "First run...\n";
174                 my ($row, $place, $ipnet, $vlan, $ip, $dip) = switch_info($switch, $distro, $port);
175                 print "Switch: $switch, Distro: $distro, vlan: $vlan\n";
176                 print "Ip net: $ipnet\n";
177                 print "Ip: $ip\n";
178
179                 do_distro($dip, $ip, $vlan, $switch);
180 #       my ($dip, $newip, $vlan) = @_;
181         }
182         close(PATCHLIST);
183 }
184
185 sub verify_run {
186         open(PATCHLIST, $patchlist) or die "Unable to open patchlist";
187         while (<PATCHLIST>) {
188                 print "Verify run....\n";
189                 my ($switch, $distro, $port) = split;
190                 
191                 my ($row, $place, $ipnet, $vlan, $ip, $dip) = switch_info($switch, $distro, $port);
192                 print "Switch: $switch, Distro: $distro, vlan: $vlan\n";
193                 print "Ip net: $ipnet\n";
194                 print "Ip: $ip\n";
195
196                 my $p = Net::Ping->new();
197                 printf "Checking if zyxel is up $dip:$vlan $ip...\n";
198                 my $counter = 0;
199                 while (1) {
200                         if ($counter > 4) {
201                                 print "No answer from $dip:$vlan $ip, trying to route it up\n";
202                                 do_distro($dip, $ip, $vlan, $switch);
203                                 my $pid = fork();
204                                 if ($pid == 0) {
205                                         sleep 100;
206                                         system("perl ./ciscong2.pl $switch");
207                                         exit 0;
208                                 }
209                                 last;
210                         }
211                         last if $p->ping($ip);
212                         print "pinging...\n";
213                         sleep 1;
214                         $counter++;
215                 }
216
217 #       my ($dip, $newip, $vlan) = @_;
218         }
219         close(PATCHLIST);
220 }
221
222
223 if ($#ARGV > -1) {
224         first_run();
225 }
226 else {
227         verify_run();
228 }
229
230 close(LOG);
231
232