Firewalld on Centos 7 with dynamic IP

I wanted to allow my home IP to much more ports then standard.

Therefore, I searched for a solution to allow a DNS name to the Firewalld.

That is not possible? It is, with a smal script I wrote and I want to share.

First, you need a dyndns provider for your home IP like

Then you create a file with my script and edit the hostname.

Also, make it executable.

Copy/paste this:

#check if file already there if not, create it
if [ -r /run/myip ]
  echo "file there"
  echo "file not there"
  dig +short > /run/myip
  firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="'$myipnew'" accept'

#get old IP
echo "$myipold"
#get new IP
myipnew=$(dig +short YOUR-DNS-NAME-HERE)
echo "$myipnew"

#compare new and old ip, do nothing if nothing changes, remove old ip and allow new IP if IP has changed

if [ "$myipold" = "$myipnew" ]
  echo "same IP"
  echo "diff IP"
  firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="'$myipold'" accept'
  firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="'$myipnew'" accept'
  dig +short YOUR-DNS-NAME-HERE > /run/myip

Now add a cron job like this running every hour:

0 * * * 0 root /home/user/ >> /var/log/myip.log