user3774714
user3774714

Reputation: 21

Net::Telnet Waitfor not working as expected

I am using Net::Telnet to login to a networking device.

My prompt is set to the following:

$telnetConsole = new Net::Telnet(
    Timeout => 5,
    Errmode => 'die',
    Prompt  => '/.*(\%|\>|\#|no\)) $/',
);

When I execute the following command on the console port of the networking device:

$telnetConsole->print('request virtual-chassis renumber member-id 0 new-member-id 4');
($prematch, $match) = $telnetConsole->waitfor(Match => 'no', Errmode=>'return', Timeout => 4);

It gives the following result:

root@switch> request virtual-chassis renumber member-id 0 new-member-id 4 

To move configuration specific to member ID 0 to member ID 4, please
use the replace command. e.g. replace pattern ge-0/ with ge-4/

Do you want to continue ? [yes,no] (no) 

{master:0}
root@switch>

As you can see I get my prompt root@switch> back, however unable to answer the sub questions [yes, or no]. I would like to know why Net::Telnet does not stop at the line Do you want to.... so I can enter yes or no. It looks like I get an extra carriage return, which causes to answer "no" and returns the prompt. I also tried with "put" and separate put statement "\n" (carriage return), however no luck.

Upvotes: 2

Views: 438

Answers (1)

TheJester1977
TheJester1977

Reputation: 155

I fought with both Net::Telnet and Net::SSH2 ad nauseam working on a script to talk to a network device. I ended up switching to Control::CLI to make it happen. In Control::CLI, this (untested) code should work (or be darned close to it):

$telnetConsoleOutput = $telnetConsole->cmd("request virtual-chassis renumber member-id 0 new-member-id 4");
$telnetConsoleOutput = $telnetConsole->cmd("yes");

Notice there's no "waitfor" in there? Control::CLI will handle that for you via the "cmd" function, at least that's been my experience. Control::CLI is a VERY nice little wrapper around connecting via telnet, ssh, and serial cable. I have used it to interact with Linux servers and networking gear, both SSH and serial. It handles a lot of stuff for you without you even knowing it, and feel confident it would resolve your issue with little rework on your end.

HTH! TheJester1977

Upvotes: 1

Related Questions