Question

I have the following program below which telnets into another device and prints serial number and Mac address.

My problem is that for some reason if I send the command once it skips the first command and sends the second, but if I copy the same command twice it will send the command.

What is the correct way to send a command multiple commands successively?

Should the buffer be flushed after every command sent ?

My Env

Eclipse Ide
Ubuntu 12.10
perl 5, version 14, subversion 2 (v5.14.2)

Snippet of my code:

$telnet = Net::Telnet->new($remoteSystem);
$| = 1;
$telnet->buffer_empty();
$telnet->buffer_empty(); 
$result = $telnet->input_log($errorlog);
#$_ = "@lines";
@TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2');
@TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2');

@mac = $telnet->cmd('ifconfig  | grep eth0 | cut -d" "  -f 11');

print "@TSN AND @TSN @mac";

print FH "$remoteSystem\n";

print "Telnetting into $remoteSystem .\n";    # Prints names of the tcd

close(telnet);
}

foreach (@host) {
    checkStatus($_);
}

OUTPUT That skips the first command:

bash-2.02  AND bash-2.02  ifconfig  | grep eth0 | cut -d" "  -f 11
00:11:D9:3C:6E:02
bash-2.02 # 
bash-2.02 Telnetting into debug79-109 .

OUTPUT That works but I have to send the same command twice:

export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2
AE20001901E2FD1
bash-2.02 # 
bash-2.02  AND export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2
AE20001901E2FD1
bash-2.02 # 
bash-2.02  ifconfig  | grep eth0 | cut -d" "  -f 11
00:11:D9:3C:6E:02
bash-2.02 # 
bash-2.02 Telnetting into debug79-109 
Was it helpful?

Solution

Specify the command prompt in your call to cmd(), e.g.@TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2', Prompt => 'bash-2.02 #');

OTHER TIPS

Try opening a connection after creating a object for the module telnet

$telnet->open($host);

After which execute waitFor method:(waits until the pattern bash-2.02 # comes)

$telnet->waitFor(/^(bash-\d+.\d+ #)$/);

and then execute your commands , it would give you proper output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top