0

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");
   }
 }

如何解决?

4

2 回答 2

0

文档

方法 login() 和 cmd() 使用对象中的提示设置来确定登录或远程命令何时完成。如果您没有正确设置提示,这些方法将因超时而失败。

也就是说,需要在创建$telnet对象的时候加上提示参数:

my $switch_name = 's-east';

my $telnet = new Net::Telnet ( 
   Timeout=>10,
   Errmode=>'return',
   Input_Log => "infile.log",
   Prompt => '/\Q$switch_name\E#\s?$/'   
);

(它会查找 shell 提示符以知道命令已完成)。

于 2013-03-04T10:47:07.467 回答
0

由于不稳定cmd(),我转而使用waitfor(String => $string)。下面的新脚本:

foreach (@linksys_sps){

 my $file = sprintf($folder."/".$_);
 print ("Create file ",$file,"\n");
 system "touch  $file";

 my $string = sprintf($_."#");
 print ("Prompt String is ",$string,"\n");

 print ("Connecting to ",$_,"\n");
 my $telnet = new Net::Telnet (
                                Timeout=>20,
                                Errmode=>'return',
                                Input_Log => "infile.log"
                                );

 $telnet->open($_);
   if ($telnet->errmsg){
   print "Can't connect to " . $_ . " Error: " . $telnet->errmsg . "\n";
   } else {

 $telnet->waitfor('/User Name:$/i');
 $telnet->print('admin');
 $telnet->waitfor('/Password:$/i');
 $telnet->print('password');
 $telnet->waitfor(String => $string );

 print ("Set Backup Terminal Variable ",$_,"\n");
 $telnet->print("terminal datadump");
 $telnet->waitfor(String => $string );

 print ("Read configuration ",$_,"\n");
 $telnet->print('sh run');
 my @config = $telnet->waitfor(String => $string );

 print ("Write configuration ",$_," to file ",$file,"\n");
 open my $fh, '>', $file or die "Cannot open file: $!";
 foreach my $config (@config) {
    print $fh "$config\n";
    }
 close $fh;

 print ("Set Default Terminal Variable ",$_,"\n");
 $telnet->print('no terminal datadump');
 $telnet->waitfor(String => $string );

 $telnet->print('exit');
    }
 }
于 2013-03-05T11:57:55.587 回答