[Tutor] adding users from a script

lonetwin lonetwin@yahoo.com
Wed, 12 Sep 2001 19:11:29 +0530


Hi,
    A suggestion ....maybe you should look at the crypt module...it 
implements the crypt(3) function and returns an encrypted passwd that the 
'useradd -p' option expects.

references:
man useradd // look at the -p option
man passwd  
man 3 crypt // this is the function that the python crypt module implements

HTH

Peace
Steve

On Wednesday 12 September 2001 18:20, you wrote:
> Hello hello hello,
>
> I haven't got the foggiest as to whether introductory messages are
> obligatory and am moving on to what's been bugging me lately skipping that
> part. Our existence is all pretty illusionary anyway, so why bother?
>
> What I'm trying to do at the moment is writing a script that would add
> users without creating a home directory, then generating and assigning
> random passwords. The proggy itself is ugly and messy and I'll have to
> work on making it presentable; but I would really like to make it
> functional first. So here goes:
>
> #!/usr/bin/python
>
> import os
>
> import whrandom
>
> rnd = whrandom.whrandom()
>
> validfirst  =
> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ23456789"
>
> pwlen = 8
>
> pw = ""
>
> idx = 0
>
> while idx < pwlen:
> 	str = validfirst[rnd.randint(0,(len(validfirst)-1))]
> 	pw = pw + str
> 	idx = idx + 1
>
> print "\nEnter your desired username:\n"
>
> nomme = raw_input (">>")
>
> os.system ('useradd ' + nomme)
>
> Now this is the part I got really stuck on
>
> os.system ('passwd ' + nomme) gets me a prompt which I need about as much
> as I need a hole in my cranium; I want the password to be read from the pw
> variable directly. How could I possibly accomplish it? From what I have
> understood from the excellent (but, sadly, a touch arcane) tome by Mark
> Lutz, I might be better off using os.popen - but the directions on its
> correct syntax are far from clear. And am I right about useradd -p
> requiring an encrypted password? Blimey, this script is probably worse off
> than the Medical Love Song protagonist...
>
>
> Pope Mickey XXIII, Chief Ov Thee Church Police
>
> -----------------------------------------------------
>
>  " The stars go waltzing out in blue and red,
>    And arbitrary blackness gallops in:
>    I shut my eyes and all the world drops dead. "
>
>     --Sylvia Plath