]> git.sesse.net Git - nms/blob - web/ssendfile.pl
Put Zyxel password in config.pm.
[nms] / web / ssendfile.pl
1 #!/usr/bin/perl
2 #
3 #
4
5 use warnings;
6 use strict;
7 use Net::Telnet;
8 use POSIX;
9
10 my $timeout = 15;
11 my $delaytime = 30;
12 my $poll_frequency = 60;
13
14 # Send a command to switch and return the data recvied from the switch
15 sub switch_exec($$) {
16         my ($cmd, $conn) = @_;
17
18         # Send the command and get data from switch
19         my @data = $conn->cmd($cmd);
20         my @lines = ();
21         foreach my $line (@data) {
22                 # Remove escape-7 sequence
23                 $line =~ s/\x1b\x37//g;
24                 push (@lines, $line);
25         }
26
27         return @lines;
28 }
29
30 sub switch_connect($) {
31         my ($ip) = @_;
32
33 #                                       Dump_Log => '/tmp/dumplog-queue',
34         my $conn = new Net::Telnet(     Timeout => $timeout,
35                                         Errmode => 'return',
36                                         Prompt => '/(es3024|e\d+\-\dsw)>/i');
37         my $ret = $conn->open(  Host => $ip);
38         if (!$ret || $ret != 1) {
39                 return (undef);
40         }
41         # XXX: Just send the password as text, I did not figure out how to
42         # handle authentication with only password through $conn->login().
43         #$conn->login(»·Prompt => '/password[: ]*$/i',
44         #               Name => $password,
45         #               Password => $password);
46         $conn->cmd($nms::config::zyxel_password);
47         # Get rid of banner
48         $conn->get;
49         return ($conn);
50 }
51
52 sub mylog {
53         my $msg = shift;
54         my $time = POSIX::ctime(time);
55         $time =~ s/\n.*$//;
56         printf STDERR "[%s] %s\n", $time, $msg;
57 }
58
59 if ($#ARGV != 1) {
60         die("Error in arguments passed\n".
61                "./ssendfile.pl addr configfile\n");
62 }
63
64 my $conn = switch_connect($ARGV[0]);
65 if (!defined($conn)) {
66         die("Could not connect to switch.\n");
67 }
68
69 open(CONFIG, $ARGV[1]);
70 while (<CONFIG>) {
71         my $cmd = $_;
72         $cmd =~ s/[\r\n]+//g;
73         print "Executing: `$cmd`\n";
74 #       if ($cmd =~ /ip ifconfig swif0 (\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3})/) {
75 #               print "New ip: $1\n";
76 #               $conn->cmd(     String => $cmd,
77 #                               Timeout => 3);
78 #               $conn = switch_connect($1);
79 #               if (!defined($conn)) {
80 #                       die "Could not connect to new ip: $1\n";
81 #               }
82 #       }
83 #       else {
84                 my @data = switch_exec($cmd, $conn);
85                 foreach my $line (@data) {
86                         $line =~ s/[\r\n]+//g;
87                         print "$line\n";
88                 }
89 #       }
90 }