Net::Telnet, script exits after connection failure

J

--Jani--

Hi all!

I'm trying to make connections (and run several commands) to several
machines with telnet.pm. Here's the example what my script does:

1. It reads all connection parameters ($ip, $username, $pwd) from
config-file into an @rray.
2. Foreach row (cell) it makes connection to $ip and logs in with
$username and $pwd.
3. It executes several commands, brings output into an @rray or file
and then exits the connection.

The problem is that if it fails to open connection (peer not respondin,
bad user etc) it exits the whole program. If I run the program without
using any telnet.pm funtions everything works fine, for example
printing variables on each cell of the @rray. So, could someone tell me
what do I have to change in telnet.pm (or in the way using it) to make
it work?

Thank you very much,
--Jani--
 
J

J. Gleixner

--Jani-- said:
Hi all!

I'm trying to make connections (and run several commands) to several
machines with telnet.pm. Here's the example what my script does:
Net::Telnet


1. It reads all connection parameters ($ip, $username, $pwd) from
config-file into an @rray.
2. Foreach row (cell) it makes connection to $ip and logs in with
$username and $pwd.
3. It executes several commands, brings output into an @rray or file
and then exits the connection.

The problem is that if it fails to open connection (peer not respondin,
bad user etc) it exits the whole program. If I run the program without
using any telnet.pm funtions everything works fine, for example
printing variables on each cell of the @rray. So, could someone tell me
what do I have to change in telnet.pm (or in the way using it) to make
it work?

Please use English, e.g. 'array' instead of @rray.

Why not show us your code?

To learn how to handle exceptions, see:
perldoc -f eval

Also, see the 'errmode' method in Net::Telnet's documentation. Seems
like you can simply set the mode to 'return'.
 
J

--Jani--

Hi!

Here's my code and configfile syntax is 192.168.0.1,username,password:

foreach (@confile) {
if (/^\#(.*)/) {
}
elsif (/^\d(.*)/) {
($swip, $swuser, $swpass) = split(/,/);
chomp ($swip, $swuser, $swpass);
use Net::Telnet;
$cmdString = "switchshow";
$brocade = new Net::Telnet (Host => "$swip", Timeout => 10);
$brocade->dump_log($tracefile);
$brocade->login(Name => $swuser, Password => $swpass);
$brocade->cmd($cmdString);
$brocade->close;
}
}
 
M

Mike Ferrari

--Jani-- said:
Hi!

Here's my code and configfile syntax is 192.168.0.1,username,password:

foreach (@confile) {
if (/^\#(.*)/) {
}
elsif (/^\d(.*)/) {
($swip, $swuser, $swpass) = split(/,/);
chomp ($swip, $swuser, $swpass);
use Net::Telnet;
$cmdString = "switchshow";
$brocade = new Net::Telnet (Host => "$swip", Timeout => 10);
$brocade->dump_log($tracefile);
$brocade->login(Name => $swuser, Password => $swpass);
$brocade->cmd($cmdString);
$brocade->close;
}
}
What is your question? Why does it exit?

Try using the "Log" features, Dump_Log, Input_Log, and Output_Log to
actually see whats going on..

Also take time to really read the perldoc.. it explains everything in
great detail.

Also.. adjust your timeout a little bit.. maybe its timing out too
early.. bump it up to 30 seconds or something.

I am assuming you have a list of devices you are connecting to.. and it
moves to the next device on the list?

Hope this helps
Mike
 
J

--Jani--

I am assuming you have a list of devices you are connecting to.. and it
moves to the next device on the list?
This is what I'm trying to do. I works fine if I'm using some other action
than those in Telnet.pm and with Telnet.pm also if all devices can be
connected. But if there's one device which does not respond, the whole
script dies.
I think I'll have to do as J. Gleixner said and try to find errmode config
and change it to return.

Jani
 
G

GAS

--Jani-- said:
I think I'll have to do as J. Gleixner said and try to find errmode config
and change it to return.

Did you try something like this ?

sub MyConnect
{
my $hostName = shift;
my $oTelnet;

$oTelnet = Net::Telnet->new( Timeout => 30,
Prompt => '/#/',
Host => $hostName,
Errmode => sub { print "Telnet
connection to $hostName fail\n"; } );
return -1 unless ( defined $oTelnet);

print "Successfully connected to $hostName\n";
$oTelnet->close();
return 0;
}


Your script die because the default Errmode is "die" command.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top