Created
May 28, 2014 07:31
-
-
Save shalk/d5d7cfa83610706c5a75 to your computer and use it in GitHub Desktop.
epxect example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[root@localhost ~]# cat 1.pl | |
#!/usr/bin/env perl | |
use 5.010; | |
use strict; | |
use Expect; | |
#use Smart::Comments; | |
my $cmd = "perl simulate.pl"; | |
my $timeout = 30; | |
my $qa = { | |
"name" => "shalk\n", | |
"age" => "17\n", | |
"home" => "China\n", | |
}; | |
my $exp = Expect->spawn($cmd) or die "can not spawn $cmd"; | |
$exp->expect( $timeout, | |
[ qr/name/ => sub { my $self =shift ; $self->send("shalk\n") ; exp_continue ;} ] , | |
[ qr/age/ => sub { my $self =shift ; $self->send("17\n") ; exp_continue ;} ] , | |
[ qr/home/ => sub { my $self =shift ; $self->send("China\n") ; exp_continue ;} ], | |
[ timeout => sub { die "simulate timeout\n";} ], | |
'-re', qr'[#>:\$] $', #' wait for shell prompt, then exit expect | |
); | |
$exp->soft_close(); | |
[root@localhost ~]# cat simulate.pl | |
#!/usr/bin/env perl | |
use 5.010; | |
my @ask = ( | |
"where are you home?", | |
"what is your name?", | |
"what is your age?" | |
); | |
for(1..10){ | |
say $ask[int rand 3]; | |
<>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment