Chinaunix首页 | 论坛 | 博客
  • 博客访问: 83744
  • 博文数量: 25
  • 博客积分: 2105
  • 博客等级: 大尉
  • 技术积分: 195
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-24 11:08
文章分类

全部博文(25)

文章存档

2013年(1)

2011年(3)

2010年(8)

2009年(13)

我的朋友
最近访客

分类:

2009-04-24 12:13:10

如果你的机器放在家中,通过一个ADSL之类的路由器连接上Internet,
当你在外面想访问家中机器时(当然首先你的路由器需要对内做DNAT或者DMZ),
就得知道路由器的对外IP,而且此IP是动态的,随时有可能自动更改,
一旦自动更改,你几乎就丢失了你的机器了,除非你打车回家 :-)
此脚本是我刚写的,它会检查路由器对外IP是否更改,如果更改了,就会自动发邮件通知你,
当你连不上你的机器了,就去查看下邮件获知新的IP,
感觉挺实用的,发上来分享,有什么意见及bug请留言赐教,谢谢 :-)

说明:
此脚本运行于家中那台机器,最好使用cron来跑,才能做到自动,我是设置每10分钟跑一次,
对本机的需求是需要安装 bash, date, sed, awk, logger, python, wget
对外部环境需求是你要有一个能用来发信的邮箱,即提供smtp服务,我用的是gamil.com
首次运行此脚本之前需修改其中的python代码,填上你的邮箱用户名及其密码,填完别忘了
将脚本权限改为 700 ,以免密码泄露.

FreeBSD 7.2 release 以及 openSUSE 11.1 均测试通过

#!/bin/bash

# written by John Shi


quit()
{
  rm -f ${prog}.pid
  exit ${1:-0}
}

tolog()
{
  local stat=(info warning error)
  sed "s/^/${stat[${1:-0}]}: /" | logger -t $prog -p user.notice
}



export LANG=C
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin

cd "$(dirname "$0")"
prog="$(basename "$0")


[ -r ${prog}.pid ] && pid=$(<${prog}.pid)
echo $$ >${prog}.pid

# if the previous process is hanging,
# just kill it, and restart my jobs.
if [ -n "$pid" ]; then
  pgid=$(ps -ww -p $pid -o pgid,args | awk -vn="$0" '$NF==n{print $1}')
  if [ -n "$pgid" ]; then
    kill -9 -$pgid
    echo "Killed the previous process[${pid}]" | tolog 1
    sleep 3
  fi 
fi


# get my ip address from website.
: >wget.log
while read -r url; do
  if wget -O ip.html "$url" 2>>wget.log; then
    getit=1
    break
  fi
done <<"EOF"
http://www.getip.com
http://www.blockstatus.com/ip-proxy-detector
http://www.lawrencegoetz.com/programs/ipinfo/
http://www.microsystools.com/services/get-ip-address/
EOF
if [ -z "$getit" ]; then
  echo "Could not get ip from website, see details below:" | tolog 2
  tolog 2

  quit 1
fi

new_ip="$(grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' ip.html | sort | uniq | xargs)"
[ -r ip.dat ] && old_ip="$(

# if the ip address is not changed, then do nothing.
[ "$new_ip" = "$old_ip" ] && quit

# detected a new ip address, sending mail now.
python 2>python.log <<EOF
import smtplib
from email.MIMEText import MIMEText

server = "smtp.gmail.com"
port = 587
user = "YOUR_ACCOUNT@gmail.com"
passwd = "YOUR_PASSOWRD"
from_addr = "NICK"
to_addr = ["YOUR_ACCOUNT@gmail.com"]
content = "Hey, XXXX's new ip might be ${new_ip}. :-)"

msg = MIMEText(content, "plain", "utf-8")
msg["Subject"] = "XXXX's new IP"
msg["From"] = from_addr
msg["To"] = to_addr[0]

srv = smtplib.SMTP(server, port)
srv.ehlo()
srv.starttls()
srv.ehlo()
srv.login(user, passwd)
srv.sendmail(from_addr, to_addr, msg.as_string())
srv.quit()
srv.close()
EOF
if [ $? -ne 0 ]; then
  echo "The Reporting Mail could not be sent, see details below:" | tolog 2
  tolog 2   quit 1
fi

# record the new ip address.
echo "$new_ip" >ip.dat
echo "HOPE's new ip might be $new_ip" | tolog

quit

阅读(1353) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:screen 配置文件

给主人留下些什么吧!~~