先自动更新cloudflare的dns记录
而后将对应的dns地址更新以下脚本的HOSTNAME
vim /root/ddns2iptables.sh
#!/bin/bash
#allow a ddns name to iptables rule
HOSTNAME=hostname
LOGFILE=/home/ip.log
Current_IP=$(host $HOSTNAME | cut -f4 -d' ')
if [ $LOGFILE = "" ] ; then
iptables -I INPUT 9 -s $Current_IP -j ACCEPT
echo $Current_IP > $LOGFILE
else
Old_IP=$(cat $LOGFILE)
if [ "$Current_IP" = "$Old_IP" ] ; then
echo IP address has not changed
else
iptables -D INPUT -s $Old_IP -j ACCEPT
iptables -I INPUT 9 -s $Current_IP -j ACCEPT
iptables-save > /etc/iptables.conf
echo $Current_IP > $LOGFILE
echo iptables have been updated
fi
fi
完成以后,iptables即可将自己的公网地址添加到现有规则,使得本地没有固定公网地址的设备可以远程获得连接的端口开放
增加一条crontab的执行命令,使得其可以每5分钟自动执行一下,确保最新的动态ip地址被登记在案
*/5 * * * * root /root/ddns2iptables.sh > /dev/null 2>&1