Solved

script to change password and email new password

Posted on 2004-10-14
3
806 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Install Predefined Certificate on Ubunto 4 48
SonarQube on Linux vs Windows 3 51
what do I need to host my own web sites? 13 49
CLI command keep running after close 7 36
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

789 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