2013-04-16 53 views
0

我有以下程序telnet到另一个设备并打印序列号和Mac地址。Perl telnet命令不发送每个命令

我的问题是,如果我发送命令一次它跳过第一个命令并发送第二个命令,但如果我复制相同的命令两次,它将发送命令。

连续发送命令多个命令的正确方法是什么?

发送每个命令后,缓冲区是否应该刷新?

我的信封

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

片段我的代码:

$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($_); 
} 

输出跳过第一个命令:

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 . 

输出工作,但我必须发送相同的命令两次:

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 

回答

0

在调用cmd()时指定命令提示符,例如, @TSN =$telnet->cmd('export | grep -e SerialNumber..[A-Z] | cut -d"\"" -f2', Prompt => 'bash-2.02 #');

+0

感谢您的回应 – user2287381

0

尝试用于模块的telnet创建对象之后打开连接

$telnet->open($host); 

在这之后执行WAITFOR方法:(等待,直到图案的bash-2.02#来)

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

然后执行你的命令,它会给你适当的输出。