中文 | English
- 自动监控公网IP变化
- 通过WxPusher推送微信通知
- 自动更新Cloudflare DNS记录
- 完善的日志记录系统
- 可配置的重试机制
- 支持多服务地址通知
/opt/ip_monitor/
├── GetPubIPAndSend.py # 主程序
├── SendMessageByWxPusher.py # 消息发送模块
├── UpdateCFDNS.py # DNS更新模块
├── config.yaml # 配置文件
├── last_ip.txt # IP记录文件
└── logs/ # 日志目录
├── ip_monitor.log # IP监控日志
├── notification.log # 通知日志
├── cloudflare_dns.log # DNS更新日志
└── cron.log # 定时任务日志
- Python 3.6+
- Linux/Windows 系统
pip install requests pyyaml
复制 config.yaml
并根据实际情况修改:
# IP检查相关配置,相关参数请到IP查询网站查看https://www.ip.cn/
ip_check:
api_url: "http://cip.cc"
last_ip_file: "./last_ip.txt"
ip_pattern: "IP\\s*:\\s*([\\d\\.]+)"
max_retries: 3
retry_delay: 2
# 通知相关配置,相关参数请到WxPubsher官网查看https://wxpusher.zjiecode.com/docs/#/
notification:
script_path: "SendMessageByWxPusher.py"
enabled: true
wxpusher:
app_token: "你的WxPusher_APP_Token"
uids: ["你的UID"]
services:
s1_port: 系统1访问端口
s2_port: 系统2访问端口
# Cloudflare配置,相关参数请到Cloudflare官网查看https://developers.cloudflare.com/api/
cloudflare:
api_token: "你的Cloudflare_API_Token"
zone_id: "你的Zone_ID"
record_name: "你的域名"
dns_type: "A"
ttl: 120
proxied: false
max_retries: 3
retry_delay: 2
# 创建工作目录
sudo mkdir /opt/ip_monitor
sudo chown $USER:$USER /opt/ip_monitor # 修改目录所有者
cd /opt/ip_monitor
# 复制所有文件到工作目录
cp /path/to/files/* .
# 设置执行权限
chmod +x *.py
# 设置配置文件权限
chmod 600 config.yaml
# 创建日志目录
mkdir logs
chmod 755 logs
# 编辑crontab
crontab -e
# 添加定时任务(每5分钟执行一次)
*/5 * * * * cd /opt/ip_monitor && /usr/bin/python3 GetPubIPAndSend.py >> /opt/ip_monitor/logs/cron.log 2>&1
sudo nano /etc/logrotate.d/ip_monitor
# 添加以下内容
/opt/ip_monitor/logs/*.log {
daily
rotate 7
compress
missingok
notifempty
}
需要修改各个Python文件中的日志配置,将日志文件路径指向 logs 目录:
# 在所有Python文件中修改日志配置
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('/opt/ip_monitor/logs/程序名.log'), # 修改为对应的日志文件名
logging.StreamHandler()
]
)
python3 GetPubIPAndSend.py
# 查看IP监控日志
tail -f /opt/ip_monitor/logs/ip_monitor.log
# 查看通知日志
tail -f /opt/ip_monitor/logs/notification.log
# 查看DNS更新日志
tail -f /opt/ip_monitor/logs/cloudflare_dns.log
# 查看定时任务日志
tail -f /opt/ip_monitor/logs/cron.log
- 请妥善保管配置文件中的敏感信息
- 建议定期检查日志文件大小
- 确保Python和相关依赖包及时更新
- 建议定期备份配置文件
- 确认Python版本:
python3 --version
- 检查依赖包:
pip3 list | grep -E "requests|pyyaml"
- 检查文件权限:
ls -l
- 检查cron日志:
grep CRON /var/log/syslog
MIT License
如有问题,请提交 Issue