in reply to Re^3: telnet from a cisco telnet
in thread telnet from a cisco telnet

finally 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;

Replies are listed 'Best First'.
Re^5: telnet from a cisco telnet
by Kumar Mantri (Novice) on Aug 09, 2011 at 11:35 UTC
    i know that the problem is at line 7
    $t->cmd("telnet 192.168.203.23");
    is that above statement is a right way to telnet from this?

      I don't think so. You get a timeout either because the connection to 192.168.203.23 is broken, or because the cmd() successfully opens a telnet connection which gets no further input. Try print() instead of cmd() and then search ( waitfor() ) the prompts and deliver the correct responses. A combination of print("telnet ...") and login() might also work, saving you some work. Take care if you are connecting to some non-UNIX hosts (see Net::Telnet : Connecting to a Remote MS-Windows Machine).

      I would attack this problem by manually logging into the first host using a telnet client (telnet or PuTTY) and then login to the second host. While doing so, I would write down every significant prompt (username, password, etc.) and the correct response to it. That way, I would gain certainty that my approach would work in principle. Then, I have everything at hand for scripting this task.

      Next step is to detect error-conditions (ill. or expired password, connectivity problems, timeouts, etc.), so your script becomes more robust.

      Update (in response to 919601, 919606):
      Try @output=$t->cmd('show system'); instead of @output=$t->print('show system');
      If you get a timeout, use cmd() with a proper Prompt => '....' argument.
      Maybe you want @output = $t->cmd(String => "show system", Prompt => '/]> /'); ?

        Hi, after executing the below code, its printing value 1 but i am expecting some set of commands, i ll show you prompts of the remote host , i did this manually before writing this code... if the telnet 192.168.203.23 the prompt is "Please enter password:" this is the last sentence it will show for entering a password and after this , it will show the prompt as "]> " ..i think i specified both correct if not help me...thank u

        use Net::Cisco; my $t =Net::Telnet::Cisco->new(Host=>'10.0.1.1'); my @output; my $tl=0; $t->login('9190','KUM@R425'); $t->print('telnet 192.168.203.23')or $tl=1; $t->waitfor('/password:/'); $t->print('public'); $t->waitfor('/]>/'); @output=$t->print('show system'); print @output; $t->waitfor('/]>/'); $t->print("exit"); open(CONFIG,">config.txt"); print CONFIG "@output\n"; close(CONFIG); $t->close;
        Dont know why its not working ...frustrated...i read the documentation of cmd() it is used to pass the commands to the remote host....thank u
        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("show system"); print @output; $t->waitfor('/]> /'); $t->print('exit'); open(CONFIG,">config.txt"); print CONFIG "@output\n"; close(CONFIG); $t->close;