公网IP检测

   脚本如下:

#!/bin/bash
#author firxiao
#monitor IP change
#date:20141128
ip_old=ip_old.txt   
ip_now=ip_now.txt
[email protected]    #发件人
[email protected]      #收件人
mail_subject=IP_MONITOR    #邮件标题
#init ip.old
while [ ! -f $ip_old ]; do
/usr/bin/curl ifconfig.me > $ip_old
done
#get ip now
/usr/bin/curl ifconfig.me > $ip_now
#compare
/usr/bin/diff $ip_now $ip_old
#if different change ip_old and send mail
if [ $? != 0 ];then
cat $ip_now >$ip_old
echo "IP has changed , the new IP is $(cat $ip_now ) !!!" |mail -s "$mail_subject" -S from=$mail_sender "$mail_user"
fi

使用方法:

   将此内容复制至 ip_monitor.sh  可自定义 修改发件人 收件人 邮件标题  

   chmod+x ip_monitor.sh

   添加计划任务

   例如每五分钟检测一次

*/5 * * * * /root/ip_monitor.sh >/dev/null 2>&1

注意: 此脚本邮件功能依赖 mail  centos安装 yum install mail



新加python版的,详见: 

http://my.oschina.net/firxiao/blog/490542 

你可能感兴趣的:(检测公网IP;IP检测)