]> git.sesse.net Git - nms/blob - clients/ciscong2.pl
Commit working ZyxelNG.
[nms] / clients / ciscong2.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.connect2" 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 do_distro {
48         my ($dip, $newip, $net, $switchname) = @_;
49
50         my $ios = Net::Telnet::Cisco->new(Host => $dip,
51                         Errmode => 'return',
52                         Prompt => '/(\S+[#>])|(es-3024>)/');
53 #                       Prompt => '/[^\s\(]+(\([^\(]\)){0,1}[#>]/');
54         if (!defined($ios)) {
55                 print "Could not connect to $dip";
56                 return;
57         }
58         $ios->login($nms::config::ios_user, $nms::config::ios_pass);
59         $ios->enable;
60
61 #nms::ios_enable($ios);
62 #$ios->cmd();
63 #nms::ios_getroute($ios, "192.168.1.0");
64 #$ios->cmd("");
65
66 #nms::ios_close($ios);
67
68 # Disable paging
69         $ios->cmd("terminal length 0");
70
71 #my @routes = $ios->cmd("show ip route");
72
73
74         $ios->print("telnet $newip");
75         $ios->waitfor("/Password:/");
76         print "Hei\n";
77         $ios->cmd($nms::config::zyxel_password);
78
79         my ($first, $second, $third, $fourth) = split(/\./, $net);
80         my $gw = "$first.$second.$third.".(int($fourth)+1);
81         $ios->cmd("ip route drop 192.168.1.0/24");
82         $ios->cmd("ip route add default $gw");
83         $ios->cmd("exit");
84
85         #system("perl ./zyxelng.pl 192.168.1.1 $newip $switchname");
86
87 ####
88
89         $ios->close();
90
91 }
92
93 ## Collect switch ips
94
95 my %switchips;
96
97 open(SWITCHES, $switches) or die "Unable to open switches";
98 while(<SWITCHES>) {
99         my ($ip, $net, $name) = split;
100
101         if ($name =~ /e\d+-\d/) {
102                 die "We only support /26 nets for now you wanted $net" if ($net ne "26");
103                 $switchips{$name} = $ip;
104         }
105 }
106 close(SWITCHES);
107
108 sub switch_info {
109         my ($switch, $distro, $port) = @_;
110
111         $switch =~ /e(\d+)-(\d)/;
112         my ($row, $place) = ($1, $2);
113         my $ipnet = $switchips{$switch};
114         my $vlan = $row . $place;
115         my ($first, $second, $third, $fourth) = split(/\./, $ipnet);
116         my $ip = "$first.$second.$third.".(int($fourth)+2);
117         my $dip = $distro.".net.tg07.gathering.org";
118
119         return ($row, $place, $ipnet, $vlan, $ip, $dip);
120 }
121
122 sub first_run {
123
124         open(PATCHLIST, $patchlist) or die "Unable to open patchlist";
125         while (<PATCHLIST>) {
126                 my ($switch, $distro, $port) = split;
127
128                 next if (defined($ARGV[0]) and $ARGV[0] ne $switch);
129                 
130                 print "First run...\n";
131                 my ($row, $place, $ipnet, $vlan, $ip, $dip) = switch_info($switch, $distro, $port);
132                 print "Switch: $switch, Distro: $distro, vlan: $vlan\n";
133                 print "Ip net: $ipnet\n";
134                 print "Ip: $ip\n";
135
136                 do_distro($dip, $ip, $ipnet, $switch);
137 #       my ($dip, $newip, $vlan) = @_;
138         }
139         close(PATCHLIST);
140 }
141
142 first_run();
143
144 close(LOG);
145
146