29 ip查询工具 并发送邮件通知目标用户

前言

这个 大概是 2020 年的事情了

呵呵 当时 我们通过 内网穿透 将局域网的服务暴露到了 互联网上

但是 存在的问题是 每隔一段时间 局域网对应的互联网 ip 就回变化

然后 如果是 在周末, 我们就很难感知到 这种变化, 并且 变化了之后 我们没有解决的方式

因此 当时产生的想法 就是定时 去查询 互联网ip, 如果 发生变化了 就基于邮件通知到 各个需要关注的目标用户

脚本内容 本身也比较简单, 这里不过多 描述

queryIpThenNotify.sh

主题框架脚本如下

查询当前网络的互联网 ip, 如果为空 或者 没有变化 记录日志之后 返回

否则 循环目标用户, 发送邮件

复制代码
# refresh env in cron shell 
source /etc/profile

appHome=$BEST_BASE/ip-query
cd $appHome
echo " current working directory : `pwd`  " >> curlLog.txt 2>&1

rm -rf ipInfo.txt

date >> curlLog.txt
./queryIp.sh >> curlLog.txt 2>&1 

# if `ip` changed 
LAST_IP=`cat ipHistory.txt | tail -n 1`
NOW_IP=`cat ipInfo.txt | tail -n 1`
if [ -z "$NOW_IP" ]; then
    echo " ip queried is empty" >> curlLog.txt 2>&1
    exit 0
fi

if [ "$LAST_IP" == "$NOW_IP" ]; then
    echo " ip is stable " >> curlLog.txt 2>&1
    exit 0 
fi
echo $NOW_IP >> ipHistory.txt

# send email to watcher 
sender="hex@yocoyt.com"
subject="ip查询结果"
toUsers=('hex@yocoyt.com' 'baifl@yocoyt.com' 'baifl198@sina.com')
for toUser in ${toUsers[@]}; do
    mailTxt="mail-$toUser.txt"
    rm -rf $mailTxt
    echo "From: $sender " >> $mailTxt
    echo "To: $toUser " >> $mailTxt
    echo "Subject: $subject " >> $mailTxt
    echo "" >> $mailTxt
    cat ipInfo.txt >> $mailTxt

    ./sendEmail.sh $sender $toUser $mailTxt >> curlLog.txt 2>&1
#    echo " sendEmail $sender -> $toUser "
done

echo "--------------------------------" >> curlLog.txt

queryIp.sh

这个查询当前网络的 互联网ip 的服务

复制代码
# curl https://ip.cn/ -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134Safari/537.36' -o ipInfo.txt

curl http://members.3322.org/dyndns/getip -o ipInfo.txt

# for debug
# echo "127.0.0.1" >> ipInfo.txt

sendEmail.sh

这个是发送邮件的具体实现

复制代码
echo "params : $*"
sender=$1
toUser=$2
mailTxt=$3
smtpServer=smtp://smtp.exmail.qq.com

senderUsername=hex@best.com
senderPassword=JerryHe123456

curl --verbose -s --url $smtpServer --mail-from $sender --mail-rcpt $toUser --upload-file $mailTxt --user "$senderUsername:$senderPassword"

使用日志

附上使用时的日志

相关推荐
Trouvaille ~16 小时前
TCP Socket编程实战(三):线程池优化与TCP编程最佳实践
linux·运维·服务器·网络·c++·网络协议·tcp/ip
JoySSLLian17 小时前
手把手教你安装免费SSL证书(附宝塔/Nginx/Apache配置教程)
网络·人工智能·网络协议·tcp/ip·nginx·apache·ssl
!chen19 小时前
LabVIEW TCP Server端工具TCP通信
网络·tcp/ip·labview
郝学胜-神的一滴19 小时前
深入解析C/S模型下的TCP通信流程:从握手到挥手的技术之旅
linux·服务器·c语言·网络·网络协议·tcp/ip
释怀不想释怀20 小时前
Linux网络基础(ip,域名)
linux·网络·tcp/ip
Trouvaille ~1 天前
【Linux】TCP Socket编程实战(一):API详解与单连接Echo Server
linux·运维·服务器·网络·c++·tcp/ip·socket
科技块儿1 天前
利用IP查询在智慧城市交通信号系统中的应用探索
android·tcp/ip·智慧城市
极新1 天前
智启新篇,智创未来,“2026智造新IP:AI驱动品牌增长新周期”峰会暨北京电子商务协会第五届第三次会员代表大会成功举办
人工智能·网络协议·tcp/ip
M158227690551 天前
TCP转LORA产品说明及应用案例
网络·网络协议·tcp/ip
2501_915106321 天前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone