]> git.sesse.net Git - nms/blob - clients/ciscong.pl
3107943c51d4c376038f5ae27acacc699c7411d6
[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 = "/home/eirikn/patchlist.txt";
16 #my $switches = "/home/eirikn/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
43 sub do_distro {
44         my ($dip, $newip, $vlan) = @_;
45
46         my $ios = Net::Telnet::Cisco->new(Host => $dip,
47                         Errmode => 'return',
48                         Prompt => '/[^\s\(]+(\([^\(]\)){0,1}[#>]/');
49         $ios->login($nms::config::ios_user, $nms::config::ios_pass);
50         $ios->enable;
51
52 #nms::ios_enable($ios);
53 #$ios->cmd();
54 #nms::ios_getroute($ios, "192.168.1.0");
55 #$ios->cmd("");
56
57 #nms::ios_close($ios);
58
59 # Disable paging
60         $ios->cmd("terminal length 0");
61
62 #my @routes = $ios->cmd("show ip route");
63
64         if (ios_getroute($ios, "192.168.1.0") == 1) {
65                 print "Already routed up 192.168.1.0/24\n" ;
66                 return;
67         }
68
69         $ios->cmd("conf t");
70         $ios->cmd("int vlan $vlan");
71         $ios->cmd("ip add 192.168.1.254 255.255.255.0 secondary");
72         $ios->cmd("exit");
73         $ios->cmd("exit");
74
75         my $zyxeloldip = "192.168.1.1";
76
77 ### Do things
78 ## ZyxelNG connect
79
80         my $p = Net::Ping->new();
81         printf "Waiting for zyxel to come up...\n";
82         while (1) {
83                 last if $p->ping($zyxeloldip);
84                 print "pinging...\n";
85                 sleep 1;
86         }
87         print "Zyxel is alive..\n";
88         $p->close();
89
90         system("perl ./zyxelng.pl 192.168.1.1 $newip");
91
92 ####
93
94         $ios->cmd("conf t");
95         $ios->cmd("int vlan $vlan");
96         $ios->cmd("no ip add 192.168.1.254 255.255.255.0 secondary");
97         $ios->cmd("exit");
98         $ios->cmd("exit");
99
100
101         $ios->close();
102
103 }
104
105
106 ## Collect switch ips
107
108 my %switchips;
109
110 open(SWITCHES, $switches) or die "Unable to open switches";
111 while(<SWITCHES>) {
112         my ($ip, $net, $name) = split;
113
114         print $name."\n";
115         if ($name =~ /e\d+-\d/) {
116                 die "We only support /26 nets for now you wanted $net" if ($net ne "26");
117                 $switchips{$name} = $ip;
118         }
119 }
120 close(SWITCHES);
121
122
123 open(PATCHLIST, $patchlist) or die "Unable to open patchlist";
124 while (<PATCHLIST>) {
125         my ($switch, $distro, $port) = split;
126         
127         $switch =~ /e(\d+)-(\d)/;
128         my ($row, $place) = ($1, $2);
129         my $ipnet = $switchips{$switch};
130         my $vlan = $row . $place;
131         print "Switch: $switch, Distro: $distro, vlan: $vlan\n";
132         print "Ip net: $ipnet\n";
133         my ($first, $second, $third, $fourth) = split(/\./, $ipnet);
134         my $ip = "$first.$second.$third.".(int($fourth)+2);
135         print "Ip: $ip\n";
136         my $dip = $distro.".net.tg07.gathering.org";
137
138         do_distro($dip, $ip, $vlan);
139 #       my ($dip, $newip, $vlan) = @_;
140 }
141 close(PATCHLIST);
142
143
144
145
146