?
Solved

script to change password and email new password

Posted on 2004-10-14
3
Medium Priority
?
838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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