仅在 IP 更改时通知
Notify only when IP changes
我使用此脚本记录分机 IP 更改
host myip.opendns.com resolver1.opendns.com | sed -n -e 's/^\(.*\)\(myip.opendns.com has address \)//p' | sed "s/^/`date` /" » /DataVolume/shares/Public/MyIP.txt
如何仅在 IP 更改时创建警报(可能创建单独的日志文件或发送邮件)?
大致如下:
#!/bin/bash
previp=
while :; do
ip=$(host myip.opendns.com resolver1.opendns.com |
sed -n '/.* has address \(.*\)/ { s///; p; q; }' )
if [[ $previp != "$ip" ]]; then
msg="$(date): IP change from '$previp' to '$ip'"
echo "$msg" >> logfile
mail -s "IP change" somebody@somewhere <<< "$msg"
previp=$ip
fi
sleep 60
done
如果您希望它只 运行 一次(例如来自 crontab):
#!/bin/bash
ipfile='/tmp/previous_ip'
ip=$(host myip.opendns.com resolver1.opendns.com |
sed -n '/.* has address \(.*\)/ { s///; p; q; }' )
if ! [[ -f $ipfile ]]; then
echo "$ip" > "$ipfile"
fi
read -r previp < "$ipfile"
if [[ $previp != "$ip" ]]; then
msg="$(date): IP change from '$previp' to '$ip'"
echo "$msg" >> logfile
mail -s "IP change" somebody@somewhere <<< "$msg"
echo "$ip" > "$ipfile"
fi
我使用此脚本记录分机 IP 更改
host myip.opendns.com resolver1.opendns.com | sed -n -e 's/^\(.*\)\(myip.opendns.com has address \)//p' | sed "s/^/`date` /" » /DataVolume/shares/Public/MyIP.txt
如何仅在 IP 更改时创建警报(可能创建单独的日志文件或发送邮件)?
大致如下:
#!/bin/bash
previp=
while :; do
ip=$(host myip.opendns.com resolver1.opendns.com |
sed -n '/.* has address \(.*\)/ { s///; p; q; }' )
if [[ $previp != "$ip" ]]; then
msg="$(date): IP change from '$previp' to '$ip'"
echo "$msg" >> logfile
mail -s "IP change" somebody@somewhere <<< "$msg"
previp=$ip
fi
sleep 60
done
如果您希望它只 运行 一次(例如来自 crontab):
#!/bin/bash
ipfile='/tmp/previous_ip'
ip=$(host myip.opendns.com resolver1.opendns.com |
sed -n '/.* has address \(.*\)/ { s///; p; q; }' )
if ! [[ -f $ipfile ]]; then
echo "$ip" > "$ipfile"
fi
read -r previp < "$ipfile"
if [[ $previp != "$ip" ]]; then
msg="$(date): IP change from '$previp' to '$ip'"
echo "$msg" >> logfile
mail -s "IP change" somebody@somewhere <<< "$msg"
echo "$ip" > "$ipfile"
fi