Skip to content

Instantly share code, notes, and snippets.

@fabiofdsantos
Forked from Khoulaiz/check_dyn_hostname
Last active April 25, 2016 12:54
Show Gist options
  • Save fabiofdsantos/68a9ee4f8fa4598e39fe4f19103a33a7 to your computer and use it in GitHub Desktop.
Save fabiofdsantos/68a9ee4f8fa4598e39fe4f19103a33a7 to your computer and use it in GitHub Desktop.
Reload iptables if... (e.g. ip was changed)
#!/bin/bash
# Reload iptables if... (e.g. ip was changed)
# Dependencies: dig (apt-get install dnsutils in debian/ubuntu)
# Note: Run "touch ~/current_ip" and add a new cron job with cmd "crontab -e" to run this script periodically.
HOSTNAME=<hostname-to-check.dyndns.org>
LOGFILE=~/current_ip
Current_IP=$(dig +short $HOSTNAME)
if [ $LOGFILE = "" ] ; then
iptables-restore < /etc/iptables/rules.v4
echo $Current_IP > $LOGFILE
else
Old_IP=$(cat $LOGFILE)
if [ "$Current_IP" != "$Old_IP" ] ; then
iptables-restore < /etc/iptables/rules.v4
echo $Current_IP > $LOGFILE
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment