Password Notification Script E-Mail Question
Hi there --
I am using a script that checks the age of the password for the user accounts on one of our servers, and if its expiration date is fourteen days or less, the user gets notified. Here is text of the script: ######################################################################### #!/bin/bash users=`grep -v ":\!\!:" /etc/shadow|grep -v ":\*." |grep -v root|cut -f1 -d:` echo $users expire_time=90 today=$((`perl -le 'print time'` / 86400 )) for user in $users ; do last_change=$((`grep $user /etc/shadow |cut -f3 -d:`)) echo $user echo $last_change days_left=$(( $expire_time - $today + $last_change )) echo $days_left if [ $days_left -lt 15 -a $days_left -ge 0 ]; then echo "passwd expired $user" mail -s "Your password on <server> will expire in $days_left days." $ user << EOF Hello, Your password of account $user, which is used for access and email on host `uname -n`, will expire in $days_left days. Please updated your password through the webmail interface at http://<hostname>/webmail. Log in using your account and passowrd, and select the Options tab. You will be taken to a screen that has a link that reads Change Password. Click on that link, and follow the onscreen prompts. Thank-you for your support. EOF fi done ######################################################################### The script works well, but I also get an email indicating that two accounts are unknown. Here is text of the message: ######################################################################### $@<server> on 12/20/2007 11:27 AM The e-mail account does not exist at the organization this message was sent to. Check the e-mail address, or contact the recipient directly to find out the correct address. < <server> #5.1.1 SMTP; 550 5.1.1 <$@server>... User unknown> user@<server>on 12/20/2007 11:27 AM The e-mail account does not exist at the organization this message was sent to. Check the e-mail address, or contact the recipient directly to find out the correct address. < <server> #5.1.1 SMTP; 550 5.1.1 <user@<server>... User unknown> ######################################################################### Does anyone have an idea what the cause is, and how I can correct it? |
If you run the script as 'sh -x scriptname' (best make the line not send mail but echo what it should do) you'll see what is needed: check output, scrub strings, that can't be (complete) user@host type addresses. The final username you can check back in /etc passwd like 'getent passwd $USERNAME 2>&1>/dev/null || { echo "Not a valid or exisiting user."; break; }' and behind the at sign you need at least four chars to make up a FQDN, IIRC. BTW next time please post your script between BB code tags for readability. Right now it doesn't look like a wellformatted script to me.
|
$ user << EOF Hello,
I hope that first space is a typo ? |
Sorry to resurrect an old thread but I am having a bit of difficulty using this script.
Whenever I try to run the script I get the following output: Code:
expire_time=90 today=15139 |
For once a valid reason for necroposting! :)
Try (the here document is in green for ease of identification; indentation and new lines are added for clarity and syntax validity; debug echos are identified as such; variable host added; some spelling and English language corrections. Not tested): Code:
#!/bin/bash |
Thank you catkin for a most excellent and prompt reply, although a slightly different error is now thrown up when I run it.
The first time round I got this: Code:
DEBUG: users: apache Code:
echo "DEBUG: user: $user: Code:
DEBUG: users: apache |
Sorry about the missing double quote; that's a risk with untested code.
At a command prompt, try running grep $user /etc/shadow |cut -f3 -d: with $user a) as pbs so grep pbs /etc/shadow |cut -f3 -d: and b) with $user set to the user after pbs in the $users list and post the output here. Probably the first will be a valid bash arithmetic expression and the second will not. The workaround will depend on what they actually are. I'm just going out for the evening ... |
Thanks for you help catkin, below is the output :)
Code:
[root@Stephenson Scripts]# grep pbs /etc/shadow |cut -f3 -d: |
Probably there is more than one user with a name including the string rca. For greater robustness, try changing grep $user /etc/shadow to grep "^$user:" /etc/shadow. That might not work well with command substitution using backticks so better change to $( ... ) which is preferred anyway so try last_change=$(( $(grep "^$user:" /etc/shadow |cut -f3 -d:) )). Actually IDK why $(( ... )) is being used when the value inside it is a simple number and not an arithmetic expression so for the neatest solution try simply last_change=$(grep "^$user:" /etc/shadow | cut -f3 -d:)
|
All times are GMT -5. The time now is 08:00 PM. |