dhunt has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to telnet routers whose ips are specified in a txt file to collect sh int output. It works well if password is incorrect but terminates if it failes to telnet.

Can someone please help me out. Many thanks!!

############################################# use Net::Telnet::Cisco; open(fd, "iplist.txt") or die "Can't open the file ip.txt"; while (<fd>) { $ip = $_; chomp $ip; $usr = 'user'; $ps = 'password'; get_connect(); get_data(); } close lst; sub get_data { $ipnfl = $ip."_raw.txt"; open(ifl,"$ipnfl"); $opnfl = $ip.".txt"; open (ofl,">$opnfl"); while (<ifl>) { if ((/^FastEthernet/) || (/^Serial/) || (/^BRI1/) || (/^Ethern +et/) ||(/^ATM/) ||(/^Tunnel/) ||(/^Multilink/) ||(/^Dialer/) ||(/^Loo +pback/) ||(/Internet address is/)) { print ofl "$hostname\t"; print ofl $_; } } close ifl; close ofl; } sub get_connect { $router_IP = $ip; $ipfl = $ip."_raw.txt"; open(fl,">$ipfl") or die "Error"; $cs = Net::Telnet::Cisco->new( Timeout => 3000, Host => $router_IP +) ; $cs->errmode("return"); $cs->login( Name =>$usr, Password => $ps); @cmd_output = $cs->cmd( 'terminal length 0' ); $hostname = $cs->last_prompt; chop $hostname; print fl "Hostname => $hostname.\n"; print fl "\n\n ::::::::::Show interface output::::::::::::: \n"; @cmd_output = $cs->cmd( 'show interface' ); print fl @cmd_output; $cs->close; close fl; } #####################################
demerphq: Added markup to make the node readable

Replies are listed 'Best First'.
Re: net telnet cisco; how to handle unable to connect
by ikegami (Patriarch) on Oct 07, 2004 at 20:28 UTC

    I think it's cause you're settingthe error mode too late. Try

    $cs = Net::Telnet::Cisco->new( Timeout => 3000, Host => $router_IP, Errmode => "return", );
Re: net telnet cisco; how to handle unable to connect
by muba (Priest) on Oct 07, 2004 at 18:53 UTC
Re: net telnet cisco; how to handle unable to connect
by hsinclai (Deacon) on Oct 07, 2004 at 19:44 UTC
    N::T::C should be inheriting a timeout from Net::Telnet ...

    [Timeout => $secs,]
    wouldn't that take care of a failed connection?

Re: net telnet cisco; how to handle unable to connect
by dave_the_m (Monsignor) on Oct 07, 2004 at 20:11 UTC
    Just as an aside, if your sole motivation is to run 'sh int' on lots of Ciscos, then you'd probably be better off using SNMP instead. See Net::SNMP.

    Dave.