Perl Basics
Introduction to Perl
FAQ's
CGI
Regular Expressions

PC Overview
Cool Stuff
My Modules
Success Stories
Links
Perl in the News
Logos

PC Internals
About
Contact
Handy Logos
What's new

CPAN Expect vs. native expect

See also
P-friendly

[ home ] - [ search ] - [ sitemap ]


Expect

CPAN Expect vs. native expect

man page
Perl Native expect
#!/usr/bin/perl -w
use Expect;
my $exp = new Expect;

my $command = 'ssh -l user 101.102.103.255';

$exp->spawn($command) or die "Cannot spawn $command: $!\n";

my $patidx = $exp->expect(30, 'Enter passphrase for key');
$exp->send("password\n");

$patidx = $exp->expect(30, 'Enter your choice');
$exp->send("r\n");

$patidx = $exp->expect(30, 'Enter IP address');
$exp->send("192.168.1.33\n");

$patidx = $exp->expect(30, 'Enter passphrase for key');
$exp->send("password\n");

$patidx = $exp->expect(30, 'hostname-zh-01 promptstring');
$exp->send("su -\n");

$exp->interact();

#!/usr/bin/expect

spawn ssh -l user 101.102.103.255

expect "Enter passphrase for key"
stty -echo
send "password\r"
stty echo

expect "Enter your choice"
send "r\r"

expect "Enter IP address"
send "192.168.1.33\r"
send "c\r"

expect "Enter passphrase for key"
send "password\r"
send "c\r"

expect "hostname-zh-01 promptstring"
send "su -\r"

interact



Getting arguments from the command line

Expect

#!/usr/bin/expect

set username [lindex $argv 0]
set password1 [lindex $argv 1]
set password2 [lindex $argv 2]

spawn ssh -l user foo.host.com
expect "password"
stty -echo
send "$password1\r"
stty echo
home - feedback - search

$Id: cms_doc_download.htm,v 1.3 2004/03/11 21:44:09 reto Exp $
© 1998-2004 reto :)