]> git.sesse.net Git - nms/blob - include/ios.pm
Bugfixes.
[nms] / include / ios.pm
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Net::Telnet;
6
7 package nms;
8
9 use base 'Exporter';
10 our @EXPORT = qw(ios_waitprompt ios_enable ios_login ios_connect ios_close);
11
12 my $iosprompt = '/[^\s]+[#>]/';
13
14 sub ios_waitprompt($) {
15         my ($t) = @_;
16
17         #while ($t->getline) {
18         #       print "Line: $_\n" if $_;
19         #}
20
21         my ($prematch, $match) = $t->waitfor($iosprompt);
22         #print "PRE: $prematch\nM: $match\n";
23 }
24
25 sub ios_enable {
26         my ($t, $enablepass) = @_;
27
28         print STDERR "Enabling...\n";
29         my @lines = $t->print("enable");
30         if ($enablepass) {
31                 $t->waitfor('/Password: /');
32                 $t->print($enablepass);
33         }
34         ios_waitprompt($t);
35 }
36
37 sub ios_login($$$) {
38         my ($t, $user, $pass) = @_;
39
40         print STDERR "Waiting username\n";
41         my ($prematch, $match) = $t->waitfor('/Username:\s{0,}/');
42         print STDERR "Sending username\n";
43         $t->print($user);
44         print STDERR "Waiting password\n";
45         ($prematch, $match) = $t->waitfor('/Password: /');
46         print STDERR "Sending password\n";
47         $t->print($pass);
48
49         ios_waitprompt($t);
50 }
51
52 use Data::Dumper;
53
54 sub ios_getroute {
55         my ($t, $net) = @_;
56
57         my @output = $t->cmd("show ip route".($net ? " $net" : ''));
58         
59         print Dumper(@output);
60         foreach (@output) {
61                 return 0 if $_ =~ /^% Network not in table/;
62         }
63
64         return 1;
65 }
66
67 sub ios_connect {
68         my ($host, $user, $pass, $enablepass) = @_;
69
70         my $t = new Net::Telnet(Timeout => 15,
71                                 Prompt => $iosprompt);
72         $t->dump_log("/tmp/ios.log");
73         $t->open($host);
74         ios_login($t, $user, $pass);
75         ios_enable($t, $enablepass) if $enablepass;
76         $t->cmd("terminal length 0"); # Don't use the pager
77
78         return $t;
79 }
80
81 sub ios_close($) {
82         my ($t) = @_;
83
84         $t->print('quit');
85         $t->close;
86 }
87