Perl telnet 不等待上一个命令结束 [英] Perl telnet does not wait for the end of the previous command

查看:33
本文介绍了Perl telnet 不等待上一个命令结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl telnet 不会等待上一个命令的结束.在文件 infile.log 中,我看到命令 my @config = $ telnet-> 的延续.cmd("sh run");,但是脚本已经开始运行命令print("Format configuration", $_, "\n");.结果,我得到一个空数组@config.

Perl telnet does not wait for the end of the previous command. In the file infile.log I see the continuation of the command my @ config = $ telnet-> cmd ("sh run");, but the script is already beginning to run the command print ("Format configuration", $ _, "\ n");. As a result, I get an empty array @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");
   }
 }

如何解决?

推荐答案

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

Due to the instability of the cmd(), I switched to using waitfor(String => $string). New script below:

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

这篇关于Perl telnet 不等待上一个命令结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆