in reply to useradd with perl and cron

I don't know for sure, but does SSH or useradd require a tty to be sitting on the receiving end? I know that the passwd program is like that. If that's the case, I'd explore the Expect module.

thor

Replies are listed 'Best First'.
Re: Re: useradd with perl and cron
by tachyon (Chancellor) on Jun 11, 2003 at 12:44 UTC

    No they don't. Nor does passwd need a tty. You can run a script like this without any tty or user input.

    #!/usr/bin/perl -w use strict; $|++; my $HOME_DIR = '/home/'; my $MAIL_DIR = '/var/mail/'; my $CRAM_FILE = '/etc/cram-md5.pwd'; my $USERADD_BIN = '/usr/sbin/useradd'; my $PASSWD_BIN = '/usr/bin/passwd'; my $PASSWD_FILE = '/etc/passwd'; my $HTPASSWD_BIN = '/usr/bin/htpasswd'; my $HTPASSWD_FILE = '/home/www/.htpasswd'; print "Username: "; chomp(my $user = <>); if(`grep /$user:/ $PASSWD_FILE`) { print "Username '$user' already exists! Continue (default=y) [y/n] +: "; exit if <> =~ /n/i; } else { `$USERADD_BIN "$user"`; } print "Shell Password: "; chomp(my $shellpwd = <>); if ($shellpwd) { open PWD, "|$PASSWD_BIN --stdin $user" or die "Can't open pipe to +$PASSWD_BIN $!\n"; print PWD $shellpwd, "\n"; close PWD; } else { print "No shell account password will be added!\n"; } $shellpwd ||= 'password'; print "POP3 password (default='$shellpwd'): "; chomp(my $pop3pwd = <>); print "Shell and POP3 passwords will be the same!\n" unless $pop3pwd; $pop3pwd ||= $shellpwd; # avoid duplicate entries in cram-md5.pwd open PWD, "<$CRAM_FILE" or die "Can't read $CRAM_FILE $!\n"; my @old_cram = grep { ! /$user\t/ } <PWD>; close PWD; print "Adding POP3 password to /etc/cram-md5.pwd\n"; open PWD, ">$CRAM_FILE" or die "Can't write $CRAM_FILE $!\n"; print PWD @old_cram, "$user\t$pop3pwd\n"; close PWD; print "Add to .htaccess (default=y) [y/n]: "; unless ( <> =~ m/n/i ) { `$HTPASSWD_BIN -b $HTPASSWD_FILE $user "$pop3pwd"`; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      You assume that all Unicies were created equal. That does not work on my Solaris 5.6 machine, whereas I assume that you're using some flavor of Linux judging by the GNU style '--stdin' parm.

      thor