Solved

script to change password and email new password

Posted on 2004-10-14
3
796 Views
Last Modified: 2013-12-16
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.
0
Comment
Question by:DMS-X
3 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 12314852
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).

0
 
LVL 1

Author Comment

by:DMS-X
ID: 12320243
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
0
 
LVL 6

Accepted Solution

by:
de2Zotjes earned 500 total points
ID: 12332868
The script you generated looks ok. There is one breaking error in it. The line where you actually set the password now reads:

passwd -x 1 --stdin < password $usern

It should read:

cat password|passwd -x1 --stdin $usern

The end of day script has a more fundamental problem: you delete the file you are reading usernames from inside the do loop. Adapt along these lines:

cd /tmp
cat userlist.txt|while read usern
do
 /usr/sbin/userdel -r $usern
done
del -f userlist.txt
/bin/mail....
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ubuntu/Asterisk after upgrade Wav issue 19 55
Migrating an SQL 2008 database to Oracle 12c 3 90
linux crontab output 3 68
Determine Who is Runnig my Bash Shell Script 4 64
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now