views:

52

answers:

1

I'm working on a project aimed at system administration for a linux installation.

I need to perform some tasks like change the user password...

I was planning to use the subprocess module for this. I'm concerned about security so, what are the 'best practices' when doing this via python?

is subprocess sufficient, or is there something better out there for the job?

[EDIT]

I should add that this is not an interactive script, it will parse the values to the system's passwd program [/EDIT]

+2  A: 

I believe the pexpect module would be the easiest way to go about this.

http://pexpect.sourceforge.net/pexpect.html

Something along these lines should work pretty well:

import pexpect
import time

def ChangePassword(user, pass):
    passwd = pexpect.spawn("/usr/bin/passwd %s" % user)

    for x in xrange(2):
        # wait for password: to come out of passwd's stdout
        passwd.expect("password: ")
        # send pass to passwd's stdin
        passwd.sendline(pass)
        time.sleep(0.1)

ChangePassword('foo', 'bar') # changes user "foo"'s password to "bar"
KingRadical
Thanks man... that looks helpful
M0E-lnx