Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

telnet from a cisco telnet

by Kumar Mantri (Novice)
on Aug 09, 2011 at 07:30 UTC ( #919406=perlquestion: print w/ replies, xml ) Need Help??
Kumar Mantri has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, i am writing a script to telnet to device by telnet session from a cisco router...ie ...telnet in a telnet..here im posting my code..the first telnet is working fine but from that session i need to telnet into a local device which is a private ip...when im using this code im getting an error that port:23 not connected...can anybody help me how to write telnet in telnet code...thanks in advance

use Net::Cisco; use Net::Telnet(); $t =Net::Telnet::Cisco->new(Host=>'$ip'); print "connected"; $t->login('', ''); $t->cmd("telnet 192.168.201.3"); $k=Net::Telnet->new(Timeout=>10,Errmode=>'die'); $k->open("192.168.201.3")or die "$k->errmsg"; print "connected"; $k->waitfor('password:$/i'); $k->print("public"); $k->waitfor('>$/i'); @output=$k->print("show system"); open(CONFIG,">config.txt"); print CONFIG "@output\n"; close(CONFIG); $k->close; $t->close;
Guys ....i am updating the above code ..finallly i got it...thanks to perlbotics here's the updated code....
use Net::Cisco; my $t =Net::Telnet::Cisco->new(Host=>'10.0.0.1'); my @output; my $tl=0; $t->login('9190','KUM@R425'); $t->print('telnet 192.168.203.23')or $tl=1; $t->waitfor('/Please enter password: /'); $t->print('public'); $t->waitfor('/]> /'); @output=$t->cmd(string=>"show system",prompt=>'/]>/'); print @output; print "$output[4]\n"; print "$output[11]\n"; $t->print('exit'); open(CONFIG,">config.csv"); print CONFIG "$output[4]^$output[11]\n"; close(CONFIG); $t->close;

Comment on telnet from a cisco telnet
Select or Download Code
Re: telnet from a cisco telnet
by Perlbotics (Canon) on Aug 09, 2011 at 08:52 UTC

    First, please do yourself a favor and use strict;

    This shouldn't work as expected since $ip is not expanded (remove the quotes): $t =Net::Telnet::Cisco->new(Host=>$ip);

    When you try to connect/telnet to 192.168.201.3 and the error message is port:23 not connected, that's not a Perl problem, but a network connectivity problem (no telnet-daemon, wrong IP, firewalls, etc.).

    The $k-session is totally useless, since you try to connect (again = 2nd parallel connection) to the private host from your local host, but not from the Cisco device. This explains the error described in the previous paragraph.

    use Net::Cisco; use Net::Telnet(); # consider: use strict; $t =Net::Telnet::Cisco->new(Host=>'$ip'); # doesn't work: '$ip' -> $i +p (BTW: where is $ip defined?) print "connected"; # a bold statement w/o any +test (e.g. checking $t) $t->login('', ''); $t->cmd("telnet 192.168.201.3"); # failure? success? no chec +k here $k=Net::Telnet->new(Timeout=>10,Errmode=>'die'); # You are opening a second +session from localhost! # Try to do the $k-stuff fr +om within the $t-session. $k->open("192.168.201.3")or die "$k->errmsg"; print "connected"; # see above $k->waitfor('password:$/i'); $k->print("public"); $k->waitfor('>$/i'); @output=$k->print("show system"); open(CONFIG,">config.txt"); # three argument open + che +ck would be nice print CONFIG "@output\n"; close(CONFIG); $k->close; $t->close;

    BTW: You can edit your nodes, e.g Reaped: telnet from a cisco telnet and do not need to create a new one when you want to update formatting or content.

      i modified this wrt your suggestions,...how about this
      use strict; use Net::Cisco; use Net::Telnet(); $t =Net::Telnet::Cisco->new(Host=>'192.168.1.1'); print "connected"; $t->login('9190', 'KUM@R425'); $t->cmd("telnet 192.168.201.3"); print "connected"; $t->waitfor('password:$/i'); $t->print("public"); $t->waitfor('>$/i'); @output=$t->print("show system"); open(CONFIG,">config.txt"); print CONFIG "@output\n"; close(CONFIG); $t->close;
        i ran the above script but it is showing the following error..
        Possible unintended interpolation of @output in string at C:\Users\Kum +ar\telnet. pl line 14. Global symbol "$t" requires explicit package name at C:\Users\Kumar\te +lnet.pl li ne 4. Global symbol "$t" requires explicit package name at C:\Users\Kumar\te +lnet.pl li ne 6. Global symbol "$t" requires explicit package name at C:\Users\Kumar\te +lnet.pl li ne 7. Global symbol "$t" requires explicit package name at C:\Users\Kumar\te +lnet.pl li ne 9. Global symbol "$t" requires explicit package name at C:\Users\Kumar\te +lnet.pl li ne 10. Global symbol "$t" requires explicit package name at C:\Users\Kumar\te +lnet.pl li ne 11. Global symbol "@output" requires explicit package name at C:\Users\Kum +ar\telnet. pl line 12. Global symbol "$t" requires explicit package name at C:\Users\Kumar\te +lnet.pl li ne 12. Global symbol "@output" requires explicit package name at C:\Users\Kum +ar\telnet. pl line 14. Global symbol "$t" requires explicit package name at C:\Users\Kumar\te +lnet.pl li ne 16.
      Can you please post the code with modifications need to be done..

        Certainly not. This is not a code writing service. In Re^3: telnet from a cisco telnet you have shown the output. Now, try to fix it step by step (e.g. use my). You can add use warnings; and use diagnostics; for better explanations of the error messages and warnings you get.
        Come back with a fixed version of your code if you still have problems. But please, show some own effort first. Thanks.

Re: telnet from a cisco telnet
by wfsp (Abbot) on Aug 09, 2011 at 08:53 UTC
    Host=>'$ip'
    Are you sure that's right? Does removing the single quotes help?

      i made some changes,it worked without errors but i got this output "command timed-out at C:\Users\Kumar\telnet.pl line 7" where did i go wrong?

      use strict; use Net::Cisco; use Net::Telnet(); my $t =Net::Telnet::Cisco->new(Host=>'10.0.0.1'); my @output=0; $t->login('9190','KUM@R425'); $t->cmd("telnet 192.168.203.23"); $t->waitfor('/:$/i'); $t->cmd("public"); $t->waitfor('/>$/i'); @output=$t->cmd("show system"); open(CONFIG,">config.txt"); print CONFIG "@output\n"; close(CONFIG); $t->close;

        yes finally i did with this..done!!but small problem,here it is writing value 1 to the text file..but declared it as array ..here im expecting some set of commands from the device.....any help please?

        use Net::Cisco; my $t =Net::Telnet::Cisco->new(Host=>'10.0.2.2'); my @output; my $tl=0; $t->login('9190','KUM@R425'); $t->print("telnet 192.168.203.23")or $tl=1; $t->waitfor('m/password:/'); $t->print("public"); $t->waitfor('m/]>/'); @output=$t->print("show system"); $t->waitfor('m/]>/'); $t->print("exit"); open(CONFIG,">config.txt"); print CONFIG "@output\n"; close(CONFIG); $t->close;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://919406]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2015-07-04 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls