Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Net::Telnet - open session if not already opened

by dasgar (Priest)
on Sep 09, 2011 at 05:25 UTC ( [id://925012]=note: print w/replies, xml ) Need Help??


in reply to Net::Telnet - open session if not already opened

First, I should point out that it looks like you might not be using use strict; in your code. If that is the case, I'd recommend adding that as well as use warnings; to your code.

Here's how I'd approach the modification:

  • Declare the $t variable as a hash outside of the foreach loop
  • For each key in your main hash, you'll be creating that key in the %t hash
  • Inside the foreach loop, do the following:
    • Check to see if the key (connection object) exists. If not, create it with unique logs.
    • Issue a command to the connection object. If there's an error, close the connection and reopen it.
    • Issue the commands that you need to do

Below is my untested modification of your code to do the above suggestions. You'll need to modify the test command to be something that makes sense for your situation.

my $continue = 1; my %t; # Hash to hold telnet connection objects # start of data collection loop that starts every X minutes while ($continue) { #New ForEach Loop to iterate through Router Hash Table foreach $router (sort (keys (%MSC_Routers))) { my $ip = $MSC_Routers{$router}; print ("$router has IP: of $ip\n\n"); #Print out which one I am connecting to print ("\t\t I am telnetting to: $ip \n\n"); sleep 3; $mode = "return"; if (!exists $t{$router}) { my $input_log = $router."_input_log"; my $dump_log = $router."_dump_log"; #Open Telnet Session $t{$router} = new Net::Telnet (Timeout => 10, input_log => "$input_log", dump_log => "$dump_log", prompt => '/#$/', Errmode => $mode ); } my $test_cmd = "ls"; if (!$t{$router}->put($test_cmd)) { # test if connection is op +en $t{$router}->close; $t{$router}->open("$ip"); } # Issue desired commands here } # end foreach $router loop } # end of while loop

Replies are listed 'Best First'.
Re^2: Net::Telnet - open session if not already opened
by isaac737 (Initiate) on Sep 09, 2011 at 23:45 UTC

    Dasgar! Thanks so much for this solution. I have beta tested it and it seems to be just what we were after.

    The key line is seeing whether or not the object is instantiated or not and I wasn't quite sure how to do that. if (!exists $t{$router}) { seems to do the trick. Oh and I do run with perl -w but I don't use strict because I can't see the purpose of it and it makes writing perl a pain. It has something to do with scope right?

    Anyhow, cheers! - Jg

Re^2: Net::Telnet - open session if not already opened
by Anonymous Monk on Jan 18, 2013 at 00:56 UTC
    Hello, can any one told me that how can i change this script with my ip address and port number... what is this line "foreach $router (sort (keys (%MSC_Routers))) {"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://925012]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found