20080416

password management in python

Problem:
How to, change existing password of USER using python.

Solution:

import crypt,random
from subprocess import Popen, PIPE
DES_SALT = list('./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
SALT = "%s%s"%(random.choice(DES_SALT),random.choice(DES_SALT))
DESHASH=crypt.crypt(PASS,SALT)
inout=Popen(["/usr/sbin/usermod","--password",DESHASH,USER],stdin=PIPE, stdout=PIPE,stderr=PIPE).communicate()

Instead of Popen one could write directly to shadow file

No comments: