Утилита empty предназначена для замены TCL/Expect, т.е. позволяет запускать и управлять интерактивными программами из sh-скриптов.
Пример кода на empty:
#!/bin/sh
empty -f -i in -o out telnet foo.bar.com
empty -w -i out -o in "ogin:" "luser"
empty -w -i out -o in "assword:" "TopSecret"
empty -s -o in "who am i"
empty -s -o in "exit"
Тоже самое на expect:
#!/usr/bin/expect
spawn telnet foo.bar.com
expect ogin {send luser\r}
expect assword {send TopSecret\r}
send "who am i\r"
send "exit\r"
expect eof
На perl:
#!/usr/bin/perl
use Expect;
my $exp = Expect->spawn("telnet foo.bar.com");
$exp->expect($timeout,
[ 'ogin: $' => sub {
$exp->send("luser\n");
exp_continue; }
],
[ 'assword:$' => sub {
$exp->send("TopSecret\n");
exp_continue; }
],
'-re', qr'[#>:] $'
);
$exp->send("who am i\n");
$exp->send("exit\n");
$exp->soft_close();
На Python:
#!/usr/local/bin/python
import pexpect
child = pexpect.spawn('telnet foo.bar.com');
child.expect('ogin: ');
child.sendline('luser');
child.expect('assword:');
child.sendline('TopSecret');
child.sendline('who am i');
child.sendline('exit');
child.expect(pexpect.EOF);
print child.before;
|