PERL-Expect:发送命令后获取输入。

发布于 2024-11-27 06:04:46 字数 421 浏览 1 评论 0原文

我正在编写一个 PERL-Expect 脚本来自动化测试。在脚本中,我想在执行命令时获取警告消息,并根据警告消息采取行动。根据某些情况,警告消息可能会有所不同,并且警告可能根本不显示。

prompt>delete fs
WARNING: Are you sure?(Y/N).. backup is running:

在上面的场景中,我需要在继续之前获取警告消息作为输入,然后在发送回复之前进行一些处理。

在某些情况下,警告可能不会显示,如下所示,例如,如果没有运行备份并且执行命令而不对其进行处理:

prompt>delete fs
Done.
prompt>show fs
...

如果显示警告消息,如何在发送命令后获取警告消息?

谢谢。

I am writing a PERL-Expect script to automate testing. In the script I want to get the warning messages when a command is executed and take action based on the warning messages. The warning messages can differ based on some situations and also the warning may not be shown at all.

prompt>delete fs
WARNING: Are you sure?(Y/N).. backup is running:

In the above scenario I need to get the WARNING message as input before proceeding and then do some processing before sending a reply.

The warning may not be displayed as shown below in some cases, for e.g., if backup is not running and command be executed without processing them:

prompt>delete fs
Done.
prompt>show fs
...

How to get the warning message after the command is send if it is displayed?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

今天小雨转甜 2024-12-04 06:04:46

Expect 出发,您想要执行以下操作:

 use Expect;

 my $exp = Expect->spawn("delete", "fs")
 or die "Cannot spawn $command: $!\n";

 $exp->expect(360,
   [ "Done." => \&report_success ],
   [ "Are you sure?(Y/N) => sub { my $self = shift;
                                   $self->send("Y\n");
                                   exp_continue; } ],
   [ "backup is running:" => \&report_failure ],
   [ timeout => \&report_timeout ],
 );

 $exp->soft_close();

Going from Expect you'd want to do something like:

 use Expect;

 my $exp = Expect->spawn("delete", "fs")
 or die "Cannot spawn $command: $!\n";

 $exp->expect(360,
   [ "Done." => \&report_success ],
   [ "Are you sure?(Y/N) => sub { my $self = shift;
                                   $self->send("Y\n");
                                   exp_continue; } ],
   [ "backup is running:" => \&report_failure ],
   [ timeout => \&report_timeout ],
 );

 $exp->soft_close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文