Link to home
Start Free TrialLog in
Avatar of DMS-X
DMS-X

asked on

script to change password and email new password

I need a bash script for Linux that will generete a random password and then change the password for account X. The password then needs to be emailed out. Is this possible.
Avatar of yuzh
yuzh

You need to use expect script to set change the user password, see my script in:
http:Q_20476614.html

You need to download expect and install it on your system, and then use "autoexpect" to create a
a setpasswd script, (I can help you to modify it). this is the key part of the  job.

In your main script:
1) To generete a random password, you need to set a rule by your self, eg, create a text file or
a very login string, and then randomly pickup a string or create a password for the user.

2) run the expect script to set the password

3) mail the password to user. (use mailx, mutt etc very easy).

Avatar of DMS-X

ASKER

To be honest this would be too much trouble (I will take a closer look however when I get a chance, I didn't completely understand what your script did). Thanks though. Take a look at what I built last night. Baybe you can offer some suggestions : )
It's not finished yet. I have not decided if it will be a cron job that automatically genereates say 20 ftp accounts a day for usage and email the username and passwords out to the users or if its user interactive and I get procmail involved in executing this script when someone sends a email with a certian header everytime they need a new ftp account. I could also build this into html then the user would enter in thier email address and exicute the script. The ftp account info would then get emailed to them.


####################################################
##need to create and change into working directory##
####################################################
mkdir /tmp/make_ftp_user
chown root:root /tmp/make_ftp_user
chmod o=rwx /tmp/make_ftp_user
cd /tmp/make_ftp_user


#################################################################
##lets use apg to create a randomly generated user and password##
#################################################################
apg -n 1 -a 0 -M l > user
apg -n 1 -a 0 -M l > password


#####################################################
##create user account from randomly genereated user##
#####################################################
cat user | while read usern
do
 /usr/sbin/adduser -m -s /bin/bash $usern
done


#########################################################
##lets now update new users password that was generated##
#########################################################
cat user | while read usern
do
 passwd -x 1 --stdin < password $usern
done


###########################################################
##lets now email randomly generated username and password##
###########################################################
cat user password \
 2>&1 |/bin/mail -s "`uname -n && date` accounting ftp user and password" user@domain.com


########################################################################
##need to copy new user name into text file for deleting accounts at end of day##
########################################################################
cat user | while read usern
do
 echo $usern >> userlist.txt
done

##########################################################
##after emailing username and password lets delete or shred files##
##########################################################
rm -f password
rm -f user


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


##END OF DAY ACCOUNT CLEANUP (not working yet)
##-----------------------------------------------------------------##
########################################################################
##at the end of every day this needs to run (delete all ftp users created and send out results to administrator)##
########################################################################
cd /tmp/
cat userlist.txt | while read usern
do
 /usr/sbin/userdel -r $usern && del -f userlist.txt \
 2>&1 |/bin/mail -s "`uname -n && date` end of day ftp user delete list" schrock@dayzed.com
done
ASKER CERTIFIED SOLUTION
Avatar of de2Zotjes
de2Zotjes
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial