Skip to main content

Posts

Showing posts from July, 2013

Python Script: Generate a random password for a Linux user, change it and automatically send the new password on email periodically

Hello Friends,

Below script changes password of a linux user and sends the new password on mail. The name of user and all email are defined in the code itself. You have to enter the program in crontab for periodic change of password. e.g. 

*/30 * * * * /home/userpass.py      ( For changing password in every half hour, put this line in crontab )

Script:


#!/root/python/Python-3.3.0/python import string import os import random import string import smtplib #user1=input('Enter the name of the user ') user1='darklord' pass1= ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(10)) cmd='echo -e "' + pass1 + '\n' + pass1 + '" | passwd ' + user1 if os.system(cmd)!= 0: print('Invalid User') else: print('New password for user ' + user1 + ' is ' + pass1 ) #SMTP_SERVER = 'smtp.gmail.com' #SMTP_PORT = 587 server = 'smtp.gmail.com' port = 587 sender = 'myemail@gmail.…