Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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
users=`grep -v ":\!\!:" /etc/shadow|grep -v ":\*." |grep -v root|cut -f1 -d:`
echo "DEBUG: users: $users"
expire_time=90
today=$((`perl -le 'print time'` / 86400 ))
host=`uname -n`
for user in $users
do
last_change=$((`grep $user /etc/shadow |cut -f3 -d:`))
echo "DEBUG: user: $user:
echo "DEBUG: last_change: $last_change"
days_left=$(( $expire_time - $today + $last_change ))
echo "DEBUG: days_left: $days_left"
if [ $days_left -lt 15 -a $days_left -ge 0 ]; then
echo "passwd expired $user"
# Note: space before EOF and user removed in line below
mail -s "Your password on $host will expire in $days_left days." $user <<EOF Hello,
Your password of account $user, which is used for access and email on $host, will expire in $days_left days.
Please update your password through the webmail interface at http://$host/webmail.
Log in using your account and password, 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 on-screen prompts.
Thank you for your support.
EOF
fi
done
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
<<TRUNCATED USERS LIST>>
./passwdcheck: line 20: unexpected EOF while looking for matching `"'
./passwdcheck: line 32: syntax error: unexpected end of file
This was rectified by simply changing this line and adding a closing quote mark at the end of the line.
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 ...
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:)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.