]> git.sesse.net Git - nms/blob - clients/ciscong.pl
277dfcf13dc7ebc712983633ac614ddb259705d0
[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
20 BEGIN {
21         require "../include/config.pm";
22         eval {
23                 require "../include/config.local.pm";
24         };
25 }
26
27 sub ios_getroute {
28         my ($t, $net) = @_;
29
30         $t->cmd("show ip route".($net ? " $net" : '')) or return 0;;
31         
32         return 1;
33 }
34
35 #my $ios_server = "noc-gw.net.tg07.gathering.org";
36 #my $vlannumber = 16;
37
38 #my $ios = nms::ios_connect($ios_server, $nms::config::ios_user, $nms::config::ios_pass)
39 #       or die "Unable to connect to cisco";
40
41
42 sub start_vlan {
43         my ($ios, $vlan) = @_;
44
45         $ios->cmd("conf t");
46         $ios->cmd("int vlan $vlan");
47         $ios->cmd("ip add 192.168.1.254 255.255.255.0 secondary");
48         $ios->cmd("exit");
49         $ios->cmd("exit");
50 }
51
52 sub stop_vlan {
53         my ($ios, $vlan) = @_;
54
55         $ios->cmd("conf t");
56         $ios->cmd("int vlan $vlan");
57         $ios->cmd("no ip add 192.168.1.254 255.255.255.0 secondary");
58         $ios->cmd("exit");
59         $ios->cmd("exit");
60 }
61
62
63
64 sub do_distro {
65         my ($dip, $newip, $vlan) = @_;
66
67         my $ios = Net::Telnet::Cisco->new(Host => $dip,
68                         Errmode => 'return',
69                         Prompt => '/[^\s\(]+(\([^\(]\)){0,1}[#>]/');
70         $ios->login($nms::config::ios_user, $nms::config::ios_pass);
71         $ios->enable;
72
73 #nms::ios_enable($ios);
74 #$ios->cmd();
75 #nms::ios_getroute($ios, "192.168.1.0");
76 #$ios->cmd("");
77
78 #nms::ios_close($ios);
79
80 # Disable paging
81         $ios->cmd("terminal length 0");
82
83 #my @routes = $ios->cmd("show ip route");
84
85         if (ios_getroute($ios, "192.168.1.0") == 1) {
86                 print "Already routed up 192.168.1.0/24\n" ;
87                 return;
88         }
89
90         my $zyxeloldip = "192.168.1.1";
91         start_vlan($ios, $vlan);
92
93 ### Do things
94 ## ZyxelNG connect
95
96         my $p = Net::Ping->new();
97         printf "Waiting for zyxel to come up...\n";
98         my $counter = 0;
99         while (1) {
100                 if ($counter > 180) {
101                         print "Waiting for zyxel on $dip:$vlan timed out, wanted to set ip: $newip";
102                         stop_vlan($ios, $vlan);
103                         return;
104                 }
105                 last if $p->ping($zyxeloldip);
106                 print "pinging...\n";
107                 sleep 1;
108                 $counter++;
109         }
110         print "Zyxel is alive..\n";
111         $p->close();
112
113         system("perl ./zyxelng.pl 192.168.1.1 $newip");
114
115 ####
116
117         stop_vlan($ios, $vlan);
118
119
120         $ios->close();
121
122 }
123
124 ## Collect switch ips
125
126 my %switchips;
127
128 open(SWITCHES, $switches) or die "Unable to open switches";
129 while(<SWITCHES>) {
130         my ($ip, $net, $name) = split;
131
132         print $name."\n";
133         if ($name =~ /e\d+-\d/) {
134                 die "We only support /26 nets for now you wanted $net" if ($net ne "26");
135                 $switchips{$name} = $ip;
136         }
137 }
138 close(SWITCHES);
139
140 sub first_run {
141
142         open(PATCHLIST, $patchlist) or die "Unable to open patchlist";
143         while (<PATCHLIST>) {
144                 my ($switch, $distro, $port) = split;
145                 
146                 $switch =~ /e(\d+)-(\d)/;
147                 my ($row, $place) = ($1, $2);
148                 my $ipnet = $switchips{$switch};
149                 my $vlan = $row . $place;
150                 print "Switch: $switch, Distro: $distro, vlan: $vlan\n";
151                 print "Ip net: $ipnet\n";
152                 my ($first, $second, $third, $fourth) = split(/\./, $ipnet);
153                 my $ip = "$first.$second.$third.".(int($fourth)+2);
154                 print "Ip: $ip\n";
155                 my $dip = $distro.".net.tg07.gathering.org";
156
157                 do_distro($dip, $ip, $vlan);
158 #       my ($dip, $newip, $vlan) = @_;
159         }
160         close(PATCHLIST);
161 }
162
163
164 if ($#ARGV > -1) {
165         first_run();
166 }
167
168
169