Perl telnet 不等待上一个命令结束

编程入门 行业动态 更新时间:2024-10-20 00:48:42
本文介绍了Perl telnet 不等待上一个命令结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 06:48:02,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   结束   Perl   telnet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!