X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=include%2Fnms.pm;h=b55314d16b64e83769b2b01faadabbda7ab6c802;hb=HEAD;hp=bfb88ad5917af5de6d1d8424f9078e9402399e57;hpb=e86389d7be5cd9e780b3b7d04316133bbbaafacf;p=nms diff --git a/include/nms.pm b/include/nms.pm index bfb88ad..b55314d 100644 --- a/include/nms.pm +++ b/include/nms.pm @@ -2,8 +2,13 @@ use strict; use warnings; use DBI; +use Net::Telnet; package nms; + +use base 'Exporter'; +our @EXPORT = qw(switch_connect switch_exec); + BEGIN { require "config.pm"; eval { @@ -21,4 +26,49 @@ sub db_connect { return $dbh; } +sub switch_connect($) { + my ($ip) = @_; + + my $conn = new Net::Telnet( Timeout => $nms::config::telnet_timeout, +# Dump_Log => '/tmp/dumplog-queue', + Errmode => 'return', +# Prompt => '/ES-3023>/'); + Prompt => '/(ES-3024|e\d{1,2}\-\dsw)>/i'); + my $ret = $conn->open( Host => $ip); + if (!$ret || $ret != 1) { + return (undef); + } + # XXX: Just send the password as text, I did not figure out how to + # handle authentication with only password through $conn->login(). + #$conn->login(»·Prompt => '/password[: ]*$/i', + # Name => $password, + # Password => $password); + $conn->cmd($nms::config::zyxel_password); + # Get rid of banner + $conn->get; + return ($conn); +} + +# Send a command to switch and return the data recvied from the switch +sub switch_exec { + my ($cmd, $conn, $print) = @_; + + # Send the command and get data from switch + my @data; + if (defined($print)) { + $conn->print($cmd); + return; + } else { + @data = $conn->cmd($cmd); + } + my @lines = (); + foreach my $line (@data) { + # Remove escape-7 sequence + $line =~ s/\x1b\x37//g; + push (@lines, $line); + } + + return @lines; +} + 1;