From: Steinar H. Gunderson Date: Wed, 29 Mar 2006 12:57:37 +0000 (+0000) Subject: Move switch_connect() into nms module. X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=9180f8278b332f8eb6c65a11d6f284f5facf6722;p=nms Move switch_connect() into nms module. --- diff --git a/include/config.pm b/include/config.pm index 1e5e5f2..e79f688 100644 --- a/include/config.pm +++ b/include/config.pm @@ -12,5 +12,6 @@ our $db_username = "nms"; our $db_password = "nms"; our $zyxel_password = "removed"; +our $telnet_timeout = 15; 1; diff --git a/include/nms.pm b/include/nms.pm index bfb88ad..f895ffd 100644 --- a/include/nms.pm +++ b/include/nms.pm @@ -2,6 +2,7 @@ use strict; use warnings; use DBI; +use Net::Telnet; package nms; BEGIN { @@ -21,4 +22,26 @@ sub db_connect { return $dbh; } +sub switch_connect($) { + my ($ip) = @_; + +# Dump_Log => '/tmp/dumplog-queue', + my $conn = new Net::Telnet( Timeout => $nms::config::telnet_timeout, + Errmode => 'return', + Prompt => '/(es3024|e\d+\-\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); +} + 1; diff --git a/web/smanagement.pl b/web/smanagement.pl index 57fa1fc..b900f1e 100755 --- a/web/smanagement.pl +++ b/web/smanagement.pl @@ -1,18 +1,11 @@ #!/usr/bin/perl -use lib '../include'; -use nms; - -# Seconds to wait for connection -my $timeout = 15; - - use warnings; use strict; -use Switch; use CGI; -use Net::Telnet; use DBI; use Data::Dumper; +use lib '../include'; +use nms; # Grab from .htaccess-authentication my $user = $ENV{'REMOTE_USER'}; @@ -45,27 +38,6 @@ sub switch_exec($$) { return @lines; } -sub switch_connect($) { - my ($ip) = @_; - - my $conn = new Net::Telnet( Timeout => $timeout, - Errmode => 'return', - Prompt => '/(es3024|e\d+\-\dsw)>/i'); - my $ret = $conn->open( Host => $ip); - if (!$ret || $ret != 1) { - return (0); - } - # 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); -} - sub parse_range($) { my $switches = $_; my @range; diff --git a/web/ssendfile.pl b/web/ssendfile.pl index 0901b8b..4ec7104 100755 --- a/web/ssendfile.pl +++ b/web/ssendfile.pl @@ -1,13 +1,9 @@ #!/usr/bin/perl -# -# - use warnings; use strict; -use Net::Telnet; +use lib '../include'; use POSIX; -my $timeout = 15; my $delaytime = 30; my $poll_frequency = 60; @@ -27,28 +23,6 @@ sub switch_exec($$) { return @lines; } -sub switch_connect($) { - my ($ip) = @_; - -# Dump_Log => '/tmp/dumplog-queue', - my $conn = new Net::Telnet( Timeout => $timeout, - Errmode => 'return', - Prompt => '/(es3024|e\d+\-\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); -} - sub mylog { my $msg = shift; my $time = POSIX::ctime(time); @@ -61,7 +35,7 @@ if ($#ARGV != 1) { "./ssendfile.pl addr configfile\n"); } -my $conn = switch_connect($ARGV[0]); +my $conn = nms::switch_connect($ARGV[0]); if (!defined($conn)) { die("Could not connect to switch.\n"); } @@ -75,7 +49,7 @@ while () { # print "New ip: $1\n"; # $conn->cmd( String => $cmd, # Timeout => 3); -# $conn = switch_connect($1); +# $conn = nms::switch_connect($1); # if (!defined($conn)) { # die "Could not connect to new ip: $1\n"; # } diff --git a/web/sshow.pl b/web/sshow.pl index 24ca128..bae9511 100755 --- a/web/sshow.pl +++ b/web/sshow.pl @@ -2,14 +2,10 @@ use lib '../include'; use nms; -# Seconds to wait for connection -my $timeout = 15; - use warnings; use strict; use Switch; use CGI; -use Net::Telnet; use DBI; # Grab from .htaccess-authentication diff --git a/web/tempfetch.pl b/web/tempfetch.pl index 76966e6..542b4d6 100644 --- a/web/tempfetch.pl +++ b/web/tempfetch.pl @@ -2,7 +2,6 @@ #use BER; use Data::Dumper; use DBI; -use Net::Telnet; use POSIX; use Time::HiRes; use lib '../include'; @@ -11,7 +10,6 @@ use strict; use warnings; # Tweak timeouit og sjekk med :support||:net hva passord vil bli -my $timeout = 25; my $location = 'skipet'; my $dbh = nms::db_connect(); @@ -70,7 +68,7 @@ while (1) { my $ip = $switch->{'ip'}; my $start = [Time::HiRes::gettimeofday]; eval { - my $conn = switch_connect($ip); + my $conn = nms::switch_connect($ip); if (!defined($conn)) { print "Could not connect to switch ".$switch->{'switch'}."\n"; } @@ -107,29 +105,6 @@ sub switch_exec { return @lines; } -sub switch_connect { - my ($ip) = @_; - - my $conn = new Net::Telnet( Timeout => $timeout, - Dump_Log => '/tmp/dumplog', - Errmode => 'return', - Prompt => '/(es3024|e\d+\-\dsw)>/i'); - my $ret = $conn->open( Host => $ip); - if (!$ret || $ret != 1) { - return (0); - } - # 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); - my @data = $conn->cmd($nms::config::zyxel_password); - # Get rid of banner - $conn->get; - return ($conn); -} - - sub mylog { my $msg = shift; my $time = POSIX::ctime(time);