Skip to content

Instantly share code, notes, and snippets.

@shalk
Created May 28, 2014 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shalk/d5d7cfa83610706c5a75 to your computer and use it in GitHub Desktop.
Save shalk/d5d7cfa83610706c5a75 to your computer and use it in GitHub Desktop.
epxect example
[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