]> git.sesse.net Git - nms/blob - web/ssendfile.pl
Move switch_connect() into nms module.
[nms] / web / ssendfile.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use lib '../include';
5 use POSIX;
6
7 my $delaytime = 30;
8 my $poll_frequency = 60;
9
10 # Send a command to switch and return the data recvied from the switch
11 sub switch_exec($$) {
12         my ($cmd, $conn) = @_;
13
14         # Send the command and get data from switch
15         my @data = $conn->cmd($cmd);
16         my @lines = ();
17         foreach my $line (@data) {
18                 # Remove escape-7 sequence
19                 $line =~ s/\x1b\x37//g;
20                 push (@lines, $line);
21         }
22
23         return @lines;
24 }
25
26 sub mylog {
27         my $msg = shift;
28         my $time = POSIX::ctime(time);
29         $time =~ s/\n.*$//;
30         printf STDERR "[%s] %s\n", $time, $msg;
31 }
32
33 if ($#ARGV != 1) {
34         die("Error in arguments passed\n".
35                "./ssendfile.pl addr configfile\n");
36 }
37
38 my $conn = nms::switch_connect($ARGV[0]);
39 if (!defined($conn)) {
40         die("Could not connect to switch.\n");
41 }
42
43 open(CONFIG, $ARGV[1]);
44 while (<CONFIG>) {
45         my $cmd = $_;
46         $cmd =~ s/[\r\n]+//g;
47         print "Executing: `$cmd`\n";
48 #       if ($cmd =~ /ip ifconfig swif0 (\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3})/) {
49 #               print "New ip: $1\n";
50 #               $conn->cmd(     String => $cmd,
51 #                               Timeout => 3);
52 #               $conn = nms::switch_connect($1);
53 #               if (!defined($conn)) {
54 #                       die "Could not connect to new ip: $1\n";
55 #               }
56 #       }
57 #       else {
58                 my @data = switch_exec($cmd, $conn);
59                 foreach my $line (@data) {
60                         $line =~ s/[\r\n]+//g;
61                         print "$line\n";
62                 }
63 #       }
64 }