Using passwd in a perl file to automate user creation

Reply

Join Date: May 2005
Posts: 3
Reputation: Deathwish is an unknown quantity at this point 
Solved Threads: 0
Deathwish Deathwish is offline Offline
Newbie Poster

Using passwd in a perl file to automate user creation

 
0
  #1
May 2nd, 2005
Hey guys,

My task is to write a perl program that will read in names/passwords from a text file and from that create user accounts and all sorts of stuff. Now I have been able to do the "stuff" and the only part giving me jip is how to overcome creating the password for the user after useradd. Using passwd on its own requires interaction to type in the password twice to confirm, and this is the part im not sure on.

  1. #Create the user account and add a password
  2. system ("/usr/sbin/useradd -m $user");
  3. system ("echo $userpass | --stdin passwd $user");

This gives me the error:

sh: -c: line 2: syntax error near unexpected token `|'
sh: -c: line 2: ` | --stdin passwd moose'

moose being the username I used :mrgreen:

It was suggested that when I had copied/pasted from a file that the char's where being corrupted so I typed the whole thing out again, to no avail.

I have investigated the possibility of using the unix_md5_crypt module to create my own encrypted passwords and use the -p tag in the useradd function but as I wont be able to install any modules on the computers this is pretty much void.
Using -p in the useradd will only add a plaintext password to the shadow file, which is useless too.

Any pointers are appreciated as I spent too long yesturday googling :-|
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Using passwd in a perl file to automate user creation

 
0
  #2
May 2nd, 2005
What type of privs do you have on the box, or do you have super user/root access? Is LDAP installed on the system already, or PAM, or Expect.pm? I'll keep digging around and see what I can't make happen, but it may come down to editing the md5 digest pm, and finding the sub that actually creates the MD5 hash, and just adding that sub into your script, and then shelling it with the md5, but let me see what I can do.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 3
Reputation: Deathwish is an unknown quantity at this point 
Solved Threads: 0
Deathwish Deathwish is offline Offline
Newbie Poster

Re: Using passwd in a perl file to automate user creation

 
0
  #3
May 2nd, 2005
Yes I have full access to my machine, but the person who will run the script wont have so extra modules being installed is a no no. Has to be done in a default env.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Using passwd in a perl file to automate user creation

 
0
  #4
May 2nd, 2005
Right, but what I'm asking is if LDAP is already installed on that box. If LDAP, or Expect.pm is on the box in question, then it will be no problem. If he does NOT have root access (which it seems he won't), then even manually opening and editing the shadow and passwd files isn't going to fly. I've got myself and 2 other people checking into this right now... it seems that passwd, along with su, demand input from the keyboard.... for security reasons. I'll keep you posted. I'm also unsure what --stdin does, I know it should be redirecting something to stdin, but what are you trying to pass this switch to?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Using passwd in a perl file to automate user creation

 
0
  #5
May 2nd, 2005
Ok, where I got confused, was that my version of passwd didn't support --stdin, which caused a big headache. Knowing that the version you are going to run it on does support --stdin (you should do a man first on passwd to make sure for the server to ensure that --stdin will work with passwd on that server). This should suffice, however, to work with --stdin:

  1. print "Enter Your Password: "; $oldpass = <STDIN>;
  2. print "Enter Your New Password: "; $newpass = <STDIN>;
  3.  
  4. open (PASSWD, "| passwd --stdin");
  5. print PASSWD "$oldpass";
  6. print PASSWD "$newpass";
  7. print PASSWD "$newpass";
  8. close(PASSWD);

This is assuming that the passwd command will require 3 lines of text. The first line being the current password, the next line being the new password, and the last line being the confirmation of the new password. There is a more complicated Method, which I have to give the huge majority of credit to Narue, which requires detaching the process from the controlling terminal using ioctl, and if needed, I'll go back through that mess, and help with that. If, however, passwd on the server this will be run, supports --stdin, that's the way to go.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 3
Reputation: Deathwish is an unknown quantity at this point 
Solved Threads: 0
Deathwish Deathwish is offline Offline
Newbie Poster

Re: Using passwd in a perl file to automate user creation

 
0
  #6
May 2nd, 2005
Nice one, thanks mate!

I'd give you a virtual beer if I could
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Using passwd in a perl file to automate user creation

 
0
  #7
May 2nd, 2005
Appreciate it.... but most of that credit goes to Narue. I put forth my stretch of effort, but without her I'd still be looking.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC