Shell脚本在密码过期之前发送电子邮件 [英] Shell script to send email before password expires
本文介绍了Shell脚本在密码过期之前发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用以下脚本的天数密码到期时,我需要通知Unix框用户.
I need to get notify the unix box users when there password going to expires in number of days for that i have used the below script.
#!/bin/sh
rcvr1=test1@testvm.localdomain.com
rcvr2=test2@testvm.localdomain.com
for i in babinlonston lonston babin
do
# convert current date to seconds
currentdate=`date +%s`
# find expiration date of user
userexp=`chage -l $i |grep 'Password expires' | cut -d: -f2`
# convert expiration date to seconds
passexp=`date -d "$userexp" +%s`
# find the remaining days for expiry
exp=`expr \( $passexp – $currentdate \)`
# convert remaining days from sec to days
expday=`expr \( $exp / 86400 \)`
if [ $expday -le 10 ]; then
echo "Please do the necessary action" | mailx -s "Password for $i will expire in $expday day/s" $rcvr3,$rcvr2
fi
done
每当我运行脚本时,都会出现以下错误.
When ever i run the script i get the below error.
[root@testvm ~]# sh script.sh
date: extra operand `23,'
Try `date --help' for more information.
expr: syntax error
expr: syntax error
script.sh: line 20: [: -le: unary operator expected
date: extra operand `+%s'
Try `date --help' for more information.
expr: syntax error
expr: syntax error
script.sh: line 20: [: -le: unary operator expected
date: extra operand `+%s'
Try `date --help' for more information.
expr: syntax error
expr: syntax error
script.sh: line 20: [: -le: unary operator expected
[root@testvm ~]#
我该如何解决这个问题.而不是-le我需要使用什么选项.
How can i slove this issue. instead of -le what option i need to use.
推荐答案
不要将其作为sh ./script运行-它将在sh shell中运行.以./script
Don't run it as sh ./script - this will run it in a sh shell. Run it as ./script
我已经对其进行了一些修改,使其更现代".
I've amended it somewhat and made it more "modern".
#!/bin/bash
#
rcvr1=test1@testvm.localdomain.com
rcvr2=test2@testvm.localdomain.com
for i in babinlonston lonston babin
do
# convert current date to seconds
currentdate=$(date +%s)
# find expiration date of user
userexp=$(chage -l $i | awk '/^Password expires/ { print $NF }')
if [[ ! -z $userexp ]]
then
# convert expiration date to seconds
passexp=$(date -d "$userexp" "+%s")
if [[ $passexp != "never" ]]
then
# find the remaining days for expiry
(( exp = passexp - currentdate))
# convert remaining days from sec to days
(( expday = exp / 86400 ))
if ((expday < 10 ))
then
echo "Please do the necessary action" | mailx -s "Password for $i will expire in $expday day/s" $rcvr3,$rcvr2
fi
fi
fi
done
这篇关于Shell脚本在密码过期之前发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文