LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1021|回复: 2

IP变了之后自动发送邮件

[复制链接]
发表于 2005-5-10 17:27:39 | 显示全部楼层 |阅读模式
#cat /bin/newip.sh
[php]
newip=[`ifconfig ppp0 | grep inet | cut -d : -f 2 | cut -d " " -f 1`]
mail $USER<<EFO
you new ip isnewip
EFO
[/php]
添加/etc/ppp/ip-up中,但要在exit 0前,
这样IP变了之后就会自动发送邮件了。
但是我不知道如何发送到我的163邮箱里!
那位兄弟指点一下
发表于 2005-5-10 21:30:25 | 显示全部楼层

  1. #!/bin/sh
  2. newip=[`ifconfig ppp0 | grep inet | cut -d : -f 2 | cut -d " " -f 1`]
  3. mail -s newip [email]email@163.com[/email] <<EFO
  4. you new ip is:$newip
  5. EFO
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-8-11 10:05:15 | 显示全部楼层
我参考鸟哥的文章,修改了一下他的脚本文件,下面的脚本是我用的,由于我没有安装sendmail还没有测试通过,不过楼上的可以帮忙看看是不是有什么问题,,不过选择接收Email的电子邮件的时候要小心些,不要被对方拒收:-)

#!/bin/bash
#
# 2. method
#    nicif  to  ADSL modem
#    outif
  method="adsl"
  nicif="eth0"
  outif="ppp0"
# 3. E-Mail
  emailto="xxx@xxx.com"

# 4. Location
  basedir="/usr/local/pingcard"
  basefile="$basedir/oldcardIP"
# 5. What jobs is your needed?
#       1. e-mail and re-dial up
  jobs="1"
#######################################################################
#====================== DETECT FILES SESSION ==========================
# 1. Detect the lynx package.
  PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
  export PATH
  if [ "$jobs" == "2" ] || [ "$jobs" == "3" ]; then
        which lynx > /dev/null 2>&1
        if [ "$?" != "0" ]; then
                echo "The nessecery package ==> lynx do not install!"
                echo "lease install this package before running this."
                exit 1
        fi
  fi
# 2. Detect the old IP file
  if [ ! -f $basefile ]; then
        echo $welcome | mail -s "Welcome using ping_net_card.sh" \
        $emailto
        /bin/touch $basefile
  fi
#######################################################################
#====================== Functions  =====================
# Get the NEW IP.
oldip () {
        oldip=`cat $basefile`
}
getip () {
        ifconfig "$outif" > /dev/null 2>&1
        if [ "$?" == "0" ]; then
                newip=`ifconfig "$outif" | grep 'inet' | \
                      awk '{print $2}' | sed -e "s/addr\://"`
        else
                newip=""
        fi
}
# Check IP
checkip () {
        if [ "$method" == "adsl" ]; then
                adsl-stop  > /dev/null 2>&1
                sleep 3s
                adsl-start > /dev/null 2>&1
        fi
        if [ "$method" == "cable" ]; then
                ifdown $nicif > /dev/null 2>&1
                sleep 3s
                ifup $nicif   > /dev/null 2>&1
        fi
        sleep 3s
}
# Email the new IP to Admin
mailtoyou () {
        echo $newip | \
        mail -s "The new IP in your $hostname" $emailto
        echo "$newip" > $basefile
}
#
#====================== ONLY EMAIL SESSION jobs 1 =====================
  if [ "$jobs" == "1" ]; then
      # 1. Get the older IP and newer IP
        oldip
        getip
        while [ "$newip" == "" ]
        do
                checkip
                getip
        done

      # 2. compare with older IP and newer IP. And e-mail to you
        if [ "$newip" != "$oldip" ]; then
                mailtoyou       # just mail to you
        fi
        exit 0
  fi
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表