This one is stumping me, I'm sure it must have been done before. I want to automte adding a user with Expect.pm (1.15)
use Expect;
use IO::Stty;
my $command = "adduser";
my @params = qw( --no-create-home --firstuid 4001 --ingroup clients --
+shell /bin/false --home /home/fred fred);
my $exp = Expect->new("$command @params") or die "Cannot spawn $comman
+d: $!\n";
$Expect::Exp_Internal = 1;
my $timeout = 5;
$exp->expect($timeout,
[
qr/UNIX password:/ => sub {
my $exp = shift;
$exp->send("sponge\n");
exp_continue;
}
],
[
qr/Full Name/ => sub {
my $exp = shift;
$exp->send("Mr Fred\n");
exp_continue;
}
],
[
qr/Number|Phone|Other/ => sub {
my $exp = shift;
$exp->send("\n");
exp_continue;
}
],
[
qr/correct/ => sub {
my $exp = shift;
$exp->send("y\n");
}
],
);
I run this and get an odd timeout:
Waiting for new data (5 seconds)...
adduser: Warning: The home dir you specified does not exist.
Adding user `fred'...
Adding new user `fred' (4001) with group `clients'.
Waiting for new data (5 seconds)...
Not creating /home/fred.
Waiting for new data (5 seconds)...
Enter new UNIX password: Waiting for new data (5 seconds)...
sponge
Waiting for new data (5 seconds)...
TIMEOUT
Returning from expect with TIMEOUT or EOF
Sometimes it gets to the password confirm line and sometimes it doesn't.