How can I set a users password in linux from a python script?
Asked Answered
F

5

6

I'm trying to automate the setup of SFTP access. This script is running as a user with sudo permissions and no password.

I can create a user like so:

>>> import subprocess
>>> process = subprocess.Popen(['sudo', 'useradd', 'test'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> process.communicate()
('', '')

Next I need to set the user's password, but I can't figure out how. Here's what I've tried.

>>> process = subprocess.Popen(['sudo', 'chpasswd'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> process.communicate('test:password')

In my python program it has no effect, in the interactive interpreter it locks up after the first line.

What's the best way to do this?

I'm running python 2.6 on Ubuntu lucid.

Fala answered 14/1, 2011 at 6:12 Comment(0)
A
6

The documentation for communicate says that you'll need to add stdin=PIPE if you're sending data to standard input via the communicate parameter:

http://docs.python.org/release/2.6/library/subprocess.html#subprocess.Popen.communicate

I appreciate this is just skeleton code, but here are another couple of other small comments, in case they are of use:

  • If you're not interested in the output of the useradd command other than whether it failed or not, you might be better off using subprocess.check_call which will raise an exception if the command returns non-zero.
  • In the second case, you should check whether process.returncode is 0 after your call to communicate('test:password')
Argue answered 14/1, 2011 at 6:32 Comment(1)
Thanks for the comments. The real code is in fact more thorough, but appreciated non the less.Fala
P
8

Try below code which will do as you required automation

from subprocess import Popen, PIPE, check_call  
check_call(['useradd', 'test'])   
proc=Popen(['passwd', 'test'],stdin=PIPE,stdout=PIPE,stderr=PIPE)  
proc.stdin.write('password\n')  
proc.stdin.write('password')  
proc.stdin.flush()  
stdout,stderr = proc.communicate()  
print stdout  
print stderr

print statements are optional.

Pokey answered 4/12, 2012 at 6:31 Comment(0)
A
6

The documentation for communicate says that you'll need to add stdin=PIPE if you're sending data to standard input via the communicate parameter:

http://docs.python.org/release/2.6/library/subprocess.html#subprocess.Popen.communicate

I appreciate this is just skeleton code, but here are another couple of other small comments, in case they are of use:

  • If you're not interested in the output of the useradd command other than whether it failed or not, you might be better off using subprocess.check_call which will raise an exception if the command returns non-zero.
  • In the second case, you should check whether process.returncode is 0 after your call to communicate('test:password')
Argue answered 14/1, 2011 at 6:32 Comment(1)
Thanks for the comments. The real code is in fact more thorough, but appreciated non the less.Fala
C
2

On Ubuntu, use usermod

class SomeClass
    def userPasswd(self, login, password):
        encPass = crypt.crypt(password)
        command = "usermod -p '{0:s}' {1:s}".format(encPass, login)
        result = os.system(command)
        if result != 0:
            logging.error(command)
        return result
Capercaillie answered 5/8, 2015 at 13:30 Comment(1)
'crypt' is deprecated and slated for removal in Python 3.13. Do not use this method.Alb
S
1

You forgot this:

stdin=subprocess.PIPE

To send data to the process, you need a stdin.

So the full statement is:

process = subprocess.Popen(['sudo', 'chpasswd'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

and then call communicate('password').

Sueannsuede answered 14/1, 2011 at 6:32 Comment(0)
A
1

I guess the issue is that you forgot the -S option for sudo.

Allene answered 27/9, 2015 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.