要求:
写一个shell脚本,通过curl -I 返回状态码来判定所访问的网站是否正常,比如当代码状态200,才算正常。测试的网站为:www.baidu.com
[root@hostlocal ~]
[root@hostlocal ~]
[root@hostlocal ~]
[root@hostlocal ~]
[root@hostlocal ~]
LOGFILE="/tmp/Email.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1
SMTP_server='smtp.qq.com'
username='XXXX@qq.com'
password='password'
from_email_address='XXXX@qq.com'
to_email_address="$1"
message_subject_utf8="$2"
message_body_utf8="$3"
message_subject_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_subject_utf8
EOF`
[ $? -eq 0 ] && message_subject="$message_subject_gb2312" || message_subject="$message_subject_utf8"
message_body_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_body_utf8
EOF`
[ $? -eq 0 ] && message_body="$message_body_gb2312" || message_body="$message_body_utf8"
sendEmail='/usr/local/bin/sendEmail'
set -x
$sendEmail -s "$SMTP_server" -xu "$username" -xp "$password" -f "$from_email_address" -t "$to_email_address" -u "$message_subject" -m "$message_body" -o message-content-type=text -o message-charset=gb2312
[root@hoatlocal ~]
[root@hoatlocal ~]
邮件通知脚本
#!/bin/bash
LOGFILE="/tmp/Email.log"
:>"$LOGFILE"
exec 1>"$LOGFILE"
exec 2>&1
SMTP_server='smtp.163.com'
username='xxxx@163.com'
password='xxxxxx'
from_email_address='xxxxxx@163.com'
to_email_address="$1"
message_subject_utf8="$2"
message_body_utf8="$3"
message_subject_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_subject_utf8
EOF`
[ $? -eq 0 ] && message_subject="$message_subject_gb2312" || message_subject="$message_subject_utf8"
message_body_gb2312=`iconv -t GB2312 -f UTF-8 << EOF
$message_body_utf8
EOF`
[ $? -eq 0 ] && message_body="$message_body_gb2312" || message_body="$message_body_utf8"
sendEmail='/usr/local/bin/sendEmail'
set -x
$sendEmail -s "$SMTP_server" -xu "$username" -xp "$password" -f "$from_email_address" -t "$to_email_address" -u "$message_subject" -m "$message_body" -o message-content-type=text -o message-charset=gb2312
测试脚本
#!/bin/bash
a=`curl -I http://www.baidu.com | awk -F " " 'NR==1{print $2}'`
if [ $a = 200 ];then
echo "访问的网站正常"
else
echo "访问的网站不正常"
/opt/sendEmail.sh 523712478@qq.com 报警 访问的网站:http://www.baidu.com不正常>,请注意!
fi