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

Hello
I am trying to create a perl script to add users to a unix front end.
Please ignore the glaring security problems for now.
Here is what I have:
#!/usr/bin/perl + use Unix::PasswdFile; + + ($sec,$min,$hour,$dy)=localtime(time); + $stamp=".".$hour.$min.$sec; + + $pw = new Unix::PasswdFile, "/etc/passwd"; $pw->user("joeblow", $pw->maxuid + 1, 10,"Joe Blow", "/export/home/ +joeblow", "/bin/ksh"); + + $pw->delete("deadguy"); + $pw->passwd("johndoe", $pw->encpass("newpass")); + foreach $user ($pw->users) { + print "Username: $user, Full Name: ", $pw->gecos($user), "\n"; + } + $pw->commit(backup => 'bak'); + $pw->commit(backup=>$stamp); + + undef $pw;

Looks like a copy of the synopsis huh?
Anyhoo - I installed the library with the make command
But still get this:

Can't call method "maxuid" without a package or object reference at adduser.pl l ine 8.


AND
Any suggestions on how to write a script like this? Validations? Share a script? Thanks


He who laughs last, doesn't get the joke.

Replies are listed 'Best First'.
Re: Passwdfile.pm help?
by Ferret (Scribe) on Aug 08, 2002 at 17:58 UTC
    $pw = new Unix::PasswdFile, "/etc/passwd";

    is a typo, for starters - seems to work fine (I'm testing it out now) if you write:

    $pw = new Unix::PasswdFile "/etc/passwd";

    instead - no comma after the package name.

    As for writing a script like this? I've been working with a 6 year old kluge for years because it's easier that what you are undertaking. It's generally a huge pain in the butt. I wish you the best, and if you have any questions, feel free to /msg me.

      Hey Ferret!
      I know this request sounds like a pain - but we have 2 groups - a very busy one that gets request for adding users and doing other work.
      And another not so busy group with less experience. We want the less experienced group to add the passwords - with out messing up the original file.

      Where are the sysadmins - well this task of adding passwords is above them.

      I was tasked to write a script to allow additions - so here I am trying (and failing badly). Just lookin' for help!

      Thanks for seeing that syntax error - I went coo coo. So simple - for others to see!

      V
      He who laughs last, doesn't get the joke.

        Oh, don't think I'm belittling your task, please! Quite the opposite - I know your pain quite well (though I am a sysadmin, and have just managed to fob off the job of password requests on our help desk)

        Now, just to clarify - you want someone to be able to do bulk password changes? Or am I missing something?

        Glad to help with the typo, not a problem at all. Lemme know how Unix::PasswordFile works for you