Welcome to bytebang » The blog about all and nothing » Find public IP address for OpenWRT via Script

Find public IP address for OpenWRT via Script

Jun 01 2018

The Problem

I was running a public server running behind my OpwnWRT firewall which was directly connected to the Internet via an LTE modem. The public IP Address of the OpenWRT Box was periodically announced via NOIP.com and so I was able to connect to it from wherever i wanted.

-----LTE----> OpenWRT(Public IP: ----> DMZ

Recently I switched my Internet provider and i got a new modem. This modem is now connected to the OpenWRT Box and acts as gateway to the Internet. It assigns a private IP Address to the WAN Port of the OpenWrt. 

-----LTE----> MODEM(Public IP: -----> OpenWRT (gets its 'public' IP via DHCP: ----> DMZ

So the DDNS Service within my firewall thinks that its public IP is now - which is in fact not true, because it should be (which is the public IP of the Modem).

Since the modem was not able to work with my preferred Free DNS Service I had to come up with a solution for this, because otherwise my exposed services can not be able accessed from the Internet anymore.

The solution

The solution was to install the package knot-dig within OpenWRT. It enables me to find my public IP address via this command:

# kdig +short myip.opendns.com @resolver1.opendns.com

Fine - the next step was to pack this script into a file which emits the current public IP address:

root@firewall:/usr/lib/ddns# cat ./getPublicIp.sh
# sample script for detecting the public IP
kdig +short myip.opendns.com @resolver1.opendns.com

root@firewall:/usr/lib/ddns# ./getPublicIp.sh

Finally I had to tell DDNS that it should use the result of the script instead of IP address that it gets assigned via the WAN interface.


And the last step was to enable port-forwarding (or DMZ) at the modem towards the firewall port.


Thats it.

Get Social

(c) 2024, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen