***** infoCopter.com/perl *****

CPAN Expect vs. native 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
© reto :)