I would like a way to update my password on a remote Ubuntu 10.4
box with fabric.
I would expect my fabfile.py
would look something like this:
def update_password(old_pw, new_pw):
# Connects over ssh with a public key authentication
run("some_passwd_cmd --old %s --new %s" % (old_pw, new_pd))
Unfortunately the only command I know of that lets one change the password is passwd
, and on Ubuntu 10.4 there doesn't seem to be any way to pass in the new (or old) password as an argument to passwd
.
What command could one use to change a user's password on Ubuntu 10.4 via fabric
?
EDIT:
I've had a look at usermod -p
, and that may work but it isn't recommended by the man page.
EDIT: For some reason usermod -p
wasn't working either over fabric.
As well, I've tried a (somewhat insecure) variation on mikej's answer that did solve the problem:
# connecting & running as root.
from fabric.api import *
from fabric.contrib import files
files.append("%s\n%s" % (passwd, passwd), '.pw.tmp')
# .pw.tmp:
# PASSWD
# PASSWD
run("passwd %s < .pw.tmp" % user)
run("rm .pw.tmp")
It's not a very elegant solution, but it works.
Thank you for reading.
Brian
You could feed the new and old passwords into passwd
using echo
e.g.
echo -e "oldpass\\nnewpass\\nnewpass" | passwd
(the -e
option for echo
enables interpretation of backslash escapes so the newlines are interpreted as such)
The trick is to use a combination of usermod
and Python’s crypt
to change your password:
from crypt import crypt
from getpass import getpass
from fabric.api import *
def change_password(user):
password = getpass('Enter a new password for user %s:' % user)
crypted_password = crypt(password, 'salt')
sudo('usermod --password %s %s' % (crypted_password, user), pty=False)
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