Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

expect and adduser

by marvell (Pilgrim)
on Oct 14, 2005 at 10:38 UTC ( [id://500189]=perlquestion: print w/replies, xml ) Need Help??

marvell has asked for the wisdom of the Perl Monks concerning the following question:

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.

--
Steve Marvell

Replies are listed 'Best First'.
Re: expect and adduser
by Perl Mouse (Chaplain) on Oct 14, 2005 at 11:17 UTC
    $ useradd --help useradd: invalid option -- - usage: useradd [-u uid [-o]] [-g group] [-G group,...] [-d home] [-s shell] [-c comment] [-m [-k template]] [-f inactive] [-e expire ] [-p passwd] [-M] [-n] [-r] +name useradd -D [-g group] [-b base] [-s shell] [-f inactive] [-e expire ]
    Note the -p option. (Yes, I am aware of the theoretical risks -- if that worries you, umount /proc).
    Perl --((8:>*

      I have to use adduser becuase of the --firstuid option

      --
      Steve Marvell

        But that's just a math problem. Long before we had all these fancy interactive user adding gadgets, I was writing automated tools to add users (with no interaction) in Perl version 3(!).

        I find it odd that you're trying to take a tool that was meant to be more handholding and interactive and make it less interactive. {grin}

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        On my system, adduser is just a symlink to useradd. And it doesn't have a firstuid option. However, if no uid is supplied on the command line, it picks the first available uid above 99, and above any other user.

        If I were to write such a tool (and I've done so in the past), I'd just open /etc/passwd and /etc/shadow for append (if needed /etc/group as well) and write the necessary lines. But that's just me. Or I use useradd/adduser to setup everything, but the password, and require the user to visit my office and type in his/her password.

        Perl --((8:>*
Re: expect and adduser
by marvell (Pilgrim) on Oct 14, 2005 at 11:44 UTC

    I've narrowed it down to passwd

    my $exp = Expect->new("passwd fred") or die "Cannot spawn: $!\n"; $Expect::Exp_Internal = 1; my $timeout = 5; $exp->expect($timeout, [ qr/UNIX password:/ => sub { my $exp = shift; $exp->send("sponge\n"); exp_continue; } ], ); $exp->soft_close();

    Get either timeout, but add a ; to the end of the command and it works fine.

    Update: that's not true, it sometimes fails

    --
    Steve Marvell

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://500189]
Approved by gube
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-29 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found