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.
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.
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:
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.process.returncode is 0 after your call to communicate('test:password')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With