From: Chris G. on
I have a script that reads in a csv files, parses the data, and uses the
info to run down a list of IP addresses and make telnet connections to
them. I do this to automate the backing up of my Foundry switch
configurations. This works great until I have an error. When I have an
error at connect time, it catches the error and reports it, just like I
want. However, it doesn't recover from the error and never makes any
more connections to the other switches, even though it says it does.

Here is my (relevant) code:

use warnings;
use strict;
use Net::Telnet;

sub GetCF;


sub GetCF {
my $telnet;
$telnet = new Net::Telnet ( Timeout=>10,
Host=>$ip,
Errmode=>\&uhoh,
);

<skipping commands executed after the connection is established>
}

sub uhoh {
if ( $@ ne "") {
print ("$@\n");
} else {
print "Success\n";
}
}
From: Martijn Lievaart on
On Mon, 05 Feb 2007 13:38:47 -0800, Chris G. wrote:

> I have a script that reads in a csv files, parses the data, and uses the
> info to run down a list of IP addresses and make telnet connections to
> them. I do this to automate the backing up of my Foundry switch
> configurations. This works great until I have an error. When I have an
> error at connect time, it catches the error and reports it, just like I
> want. However, it doesn't recover from the error and never makes any
> more connections to the other switches, even though it says it does.

I use rancid for this. Actually I use it to backup the configs, but when
it's installed, it's a great way to run commands on any of the installed
network devices. And obviously parse the results in Perl.

And I actually had bad experiences with Net::Telnet to access Cisco
routers. It would sometimes just act weird. Never got to the bottom of it
though.

HTH,
M4
From: DJ Stunks on
On Feb 5, 2:38 pm, "Chris G." <nos...(a)example.com> wrote:
> I have a script that reads in a csv files, parses the data, and uses the
> info to run down a list of IP addresses and make telnet connections to
> them. I do this to automate the backing up of my Foundry switch
> configurations. This works great until I have an error. When I have an
> error at connect time, it catches the error and reports it, just like I
> want. However, it doesn't recover from the error and never makes any
> more connections to the other switches, even though it says it does.
>
> Here is my (relevant) code:
>
> use warnings;
> use strict;
> use Net::Telnet;
>
> sub GetCF;
>
> sub GetCF {
> my $telnet;
> $telnet = new Net::Telnet ( Timeout=>10,
> Host=>$ip,
> Errmode=>\&uhoh,
> );
>
> <skipping commands executed after the connection is established>
>
> }
>
> sub uhoh {
> if ( $@ ne "") {
> print ("$@\n");
> } else {
> print "Success\n";
> }
>
> }

how about something like this:

#!/usr/bin/perl

use warnings;
use strict;

use Net::Telnet;

my $t = Net::Telnet->new( Timeout => 10,
Errmode => sub { warn $_[0] },
);

HOST:
while ( my ($user,$pass,$host) = split /[,\n]/, <DATA> ) {

$t->open( $host ) or next HOST;
$t->login( $user,$pass ) or next HOST;

# do something with $t telnet session

$t->close;
}

__DATA__
user1,pass1,host1.example.com
user2,pass2,host1.example.com
user1,pass1,host2.example.com
user2,pass2,host2.example.com

-jp

PS - in the future please post only complete scripts including sample
data. if this means you have to do a little extra work putting
together a good question, you will be the better for it :-)

From: Chris G. on
DJ,

I would have posted the entire script, but it's >220 lines of code. Do
you really want all that code??? I will post it, if you want. I
understand the reasoning behind your request. I was just trying to save
some bandwidth for others.

DJ Stunks wrote:

> PS - in the future please post only complete scripts including sample
> data. if this means you have to do a little extra work putting
> together a good question, you will be the better for it :-)
>
From: Chris G. on
Hi,

I like this idea and see that I did not provide enough information. I
apologize. But, in what you have given me, this is a good start for
future thinking.

In the code below, you do the
$t->open( $host ) or next HOST;

If I use that, what happens to that current attempt to open the telnet
session? Does that just go away and the script continues? I will get
some more info and post it to help clarify why I ask.

Regards,

Chris


DJ Stunks wrote:
> how about something like this:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> use Net::Telnet;
>
> my $t = Net::Telnet->new( Timeout => 10,
> Errmode => sub { warn $_[0] },
> );
>
> HOST:
> while ( my ($user,$pass,$host) = split /[,\n]/, <DATA> ) {
>
> $t->open( $host ) or next HOST;
> $t->login( $user,$pass ) or next HOST;
>
> # do something with $t telnet session
>
> $t->close;
> }
>
> __DATA__
> user1,pass1,host1.example.com
> user2,pass2,host1.example.com
> user1,pass1,host2.example.com
> user2,pass2,host2.example.com
>
> -jp