Solved

script to change password and email new password

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sed question 2 49
People Counting Software 2 62
Which Linux flavors will this run on? 6 37
Virtualizing very old guest OS 4 34
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

12 Experts available now in Live!

Get 1:1 Help Now