Willkommen bei 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: 123.45.12.34) ----> 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: 123.45.12.34) -----> OpenWRT (gets its 'public' IP via DHCP: 192.168.1.50) ----> DMZ

So the DDNS Service within my firewall thinks that its public IP is now 192.168.1.50 - which is in fact not true, because it should be 123.45.12.34 (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
178.114.180.44

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
#!/bin/sh
# sample script for detecting the public IP
kdig +short myip.opendns.com @resolver1.opendns.com


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

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.

ddns_cfg_luci.png

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

portforwarding_ha35.png

Thats it.

Get Social


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