views:

73

answers:

3

How do I create a user in Linux using Python? I mean, I know about the subprocess module and thought about calling 'adduser' and passing all the parameters at once, but the 'adduser' command asks some questions like password, full name, phone and stuff. How would I answer this questions using subprocess? I've seen module called pexpect in this question: http://stackoverflow.com/questions/209470/can-i-use-python-as-a-bash-replacement. Is there any other standard module?

+1  A: 

You could just use the built-in binaries so just call useradd or something through the subprocess module, However I don't know if there's any other modules that hook into Linux to provide such functionality.

Xeross
+6  A: 

Use useradd, it doesn't ask any questions but accepts many command line options.

Riateche
+2  A: 

On Ubuntu, you could use the python-libuser package

unutbu
Is this a ubuntu-only package? If I need to run it on other distros, will I have to manually install this package?
Salsa
You are probably better off with `useradd` for cross-distribution compatibility, but note that even `useradd` is not guaranteed to be available on all distros. Puppy Linux I believe does not come with useradd by default, for example.
unutbu