
OpenWRT
1 前言
一个问题,一篇文章,一出故事。
笔者最近部署了OpenWRT路由环境,由于笔者的域名使用花生壳动实现动态域名解析,即DDNS。
笔者想通过http协议脚本刷新路由出口的IP,于是整理本章节。
2 最佳实践
2.1 openWRT环境
2.2 部署DDNS刷新脚本
2.2.1 安装软件包
opkg update opkg install curl bind-host
2.2.2 创建脚本
mkdir -p ~/scripts vim ~/scripts/pusoray.sh
创建如下脚本,
#!/bin/bash domain="example.cmdschool.org" user="username" pwd="usernamepwd" ipLog="/var/log/ip.log" nowIP=$(curl -s icanhazip.com || curl -s ifconfig.me) if ! echo "$nowIP" | grep -Eq '^([0-9]{1,3}\.){3}[0-9]{1,3}$'; then exit 1 fi if ! command -v nslookup >/dev/null; then echo 'Please install bind-host first!' >&2 exit 1 fi if nslookup "$domain" | grep -q "$nowIP"; then echo "DNS don't need to updated" >&2 exit fi for i in {1..3}; do response=$(curl -s "http://$user:$pwd@ddns.oray.com/ph/update?hostname=$domain") echo "$response" | grep -q "good" && break sleep 5 done echo "$(date +"%Y-%m-%d %H:%M:%S") $nowIP" >> "$ipLog"
注意需要更改的参数:
– 参数“domain”,是你向花生壳申请的域名
– 参数“user”,登录花生壳的账号
– 参数“pwd”,登录花生壳的密码
2.2.3 测试脚本
ash ~/scripts/pusoray.sh
2.2.4 设计脚本触发
crontab -e
加入如下内容(以下每5分钟执行一次):
*/5 * * * * ash ~/scripts/pusoray.sh
没有评论