| CLI Automatic Test with Perl | ||
|---|---|---|
| <<< Previous | Next >>> | |
Mulitpattering against timeout.
It's all about $obj->expect.
Example 1. Same SSH connection just with more expect-ations ;)
      #!/usr/bin/perl -w
      use strict;
      use Expect;
      my $timeout = 5;
      my $spawn;
      my $ssh = Expect->new();
      $ssh->spawn("ssh root\@127.0.0.1") or die "Can not spawn ssh: $!\n";
      $ssh->expect($timeout,
	     [
	      '-re', '\(yes\/no\)\?\s',
	      sub {
		$spawn = 1;
		my $fh = shift;
		$fh->send("yes\r");
		exp_continue;
	      }
	     ],
	     [
	      '-re', 'password:\s',
	      sub {
		my $fh = shift;
		$fh->send("password\r");
	      }
	     ],
	     [
	      eof =>
	      sub {
		if ($spawn) {
		  warn "EOF while login.\n";
		} else {
		  warn "EOF while spawning ssh.\n";
		}
	      }
	     ],
	     [
	      timeout =>
	      sub {
		if ($spawn) {
		  warn "No prompt.\n";
		} else {
		  warn "Can't spawn ssh\n";
		}
	      }
	     ],
	     '-re', '\[root\sprompt\]#\s',
	    ) or
         die "Expect failed at: ".$ssh->exp_error()."\n";
     $ssh->interact();
     | 
| <<< Previous | Home | Next >>> | 
| 5. Expect - basics | 7. Expect STDOUT/STDERR |