Perl telnet 不等待上一个命令的结束。在文件 infile.log 我看到命令的延续my @ config = $ telnet-> cmd ("sh run");
,但脚本已经开始运行命令print ("Format configuration", $ _, "\ n");
。结果,我得到一个空数组@config。
foreach (@linksys_sps){
print ("Connecting to ",$_,"\n");
my $telnet = new Net::Telnet ( Timeout=>10,Errmode=>'return',Input_Log => "infile.log");
$telnet->open($_);
if ($telnet->errmsg){
print "Can't connect to " . $_ . " Error: " . $telnet->errmsg . "\n";
} else {
$telnet->max_buffer_length(5 * 1024 * 1024);
$telnet->waitfor('/User Name:$/i');
$telnet->print('admin');
$telnet->waitfor('/Password:$/i');
$telnet->print('password');
print ("Set Terminal Variable ",$_,"\n");
$telnet->cmd("terminal datadump");
print ("Create file ",$_,"\n");
my $file = sprintf($folder."/".$_);
print ("Create file ",$file,"\n");
system "touch $file";
system "chown nobody $file";
print ("Read configuration ",$_,"\n");
my @config = $telnet->cmd("sh run");
print ("Write configuration ",$_," to file ",$file,"\n");
open my $fh, "> $file" or die "Can't open $file : $!";
foreach (@config) {
print $fh "$_"; # Print each entry in our array to the file
}
close $fh;
print ("Set Terminal Variable ",$_,"\n");
$telnet->cmd("no terminal datadump");
$telnet->cmd("exit");
}
}
如何解决?