]> git.sesse.net Git - nms/blob - clients/switchport.pl
Update make-switches.pl for TG07.
[nms] / clients / switchport.pl
1 #!/usr/bin/perl
2 #
3 #
4
5 use warnings;
6 use strict;
7 use Net::Telnet;
8
9 # Tweak and check
10 my $password = 'removed';
11 my $timeout = 15;
12
13 sub switch_exec($$) {
14         my ($cmd, $conn) = @_;
15
16         # Send the command and get data from switch
17         my @data = $conn->cmd($cmd);
18         my @lines = ();
19         foreach my $line (@data) {
20                 # Remove escape-7 sequence
21                 $line =~ s/\x1b\x37//g;
22                 push (@lines, $line);
23         }
24
25         return @data;
26 }
27
28 sub switch_connect($) {
29         my ($ip) = @_;
30
31 #                                       Errmode => 'return',
32         my $conn = new Net::Telnet(     Timeout => $timeout,
33                                         Dump_Log => 'dumplog',
34                                         Prompt => '/es(\-)?3024|e(\-)?\d+\-\dsw>/i');
35         my $ret = $conn->open(  Host => $ip);
36         if (!$ret || $ret != 1) {
37                 return (undef);
38         }
39         # XXX: Just send the password as text, I did not figure out how to
40         # handle authentication with only password through $conn->login().
41         #$conn->login(»·Prompt => '/password[: ]*$/i',
42         #              Name => $password,
43         #              Password => $password);
44         $conn->cmd($password);
45         # Get rid of banner
46         $conn->get;
47         return ($conn);
48 }
49
50 sub switch_get_arp($) {
51         my ($conn) = @_;
52
53         my @data = switch_exec('ip arp status', $conn);
54         foreach (@data) {
55 # 81.162.241.1    Ethernet       300   00:e0:2b:e0:f3:00 41   swif0 swp24
56                 $_ =~ /(\d+\.\d+\.\d+\.\d+)\s+\w+\s+\d+\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)\s+\d+\s+\w+\s+(\w+)/;
57                 next if (!defined($1));
58                 next if ($3 eq "NULL");
59                 print "$1 $2 $3\n";     
60         }
61         return @data;
62 }
63
64 my $conn = switch_connect($ARGV[0]);
65 if (!defined($conn)) {
66         die "something went wrong!";
67 }
68 my @data = switch_exec('sys monitor status', $conn);
69 my @fields = split(/\s+/, $data[2]);
70 # The temp fields are 6, 7, 8
71 print "$fields[7] + $fields[8] + $fields[9]\n";
72 my $avgtemp = ($fields[7] + $fields[8] + $fields[9]) / 3;
73 print $avgtemp." avgtemp\n";
74 $conn->close();
75