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