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