What's new

TIP: How to get your public ipv4 address programmatical

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Poul Bak

Occasional Visitor
Of course you can open the WebUI to get your public ip address, but how about getting it programmatical?
Actually, it's simple, just add this line to '/jffs/configs/dnsmasq.conf.add':

interface-name=publicip.local,eth0/4

Now you can call:

nslookup publicip.local

or use for instance .Net's Dns class and get your public ip address as a standard dns lookup
This also works for dynamic public ip addresses.
Off course this only works on your lan.

Hope this can be useful to someone.
 
Interesting idea. I imagine you can make it more flexible for different configurations by customizing it in dnsmasq.postconf. Assumes a LAN domain is configured, but doesn't support Dual-WAN situations.
Code:
#!/bin/sh
source /usr/sbin/helper.sh
CONFIG="$1"
pc_append "interface-name=publicip.$(nvram get lan_domain),$(nvram get wan0_ifname)/4" "$CONFIG"
 
Of course you can open the WebUI to get your public ip address, but how about getting it programmatical?
Actually, it's simple, just add this line to '/jffs/configs/dnsmasq.conf.add':

interface-name=publicip.local,eth0/4

Now you can call:

nslookup publicip.local

or use for instance .Net's Dns class and get your public ip address as a standard dns lookup
This also works for dynamic public ip addresses.
Off course this only works on your lan.

Hope this can be useful to someone.
Does this work if your router is in Double NAT? I suspect it won't. You can use something like
Code:
curl ipecho.net/plain
instead
 
Does this work if your router is in Double NAT?
Dnsmasq must be configured on the router, that's connected to the internet, then it will work whereever you local dns works.

I just tested on a VirtualBox VM, connected to host via NAT. The host is connected to the router using NAT, too. It works fine.
 
Last edited:
Dnsmasq must be configured on the router, that's connected to the internet, then it will work whereever you local dns works.

I just tested on a VirtualBox VM, connected to host via NAT. The host is connected to the router using NAT, too. It works fine.
It may not work if your router is behind an ISP router (double NAT), where the Asus eth0 interface is assigned a private IP address by the ISP router and the ISP router has the public IP address.
 
It may not work if your router is behind an ISP router (double NAT), where the Asus eth0 interface is assigned a private IP address by the ISP router and the ISP router has the public IP address.
That is correct, in that case the router doesn't know the public IP.
You will have to use the traditional method of quering a server on the internet
 
Believe it not, a few people actually use something called Windows :D

That's a script that's in the router's firmware. It uses stun, which allows it to retrieve the public IP even if behind a double NAT or CGNAT. You could use it in a dnsmasq.postconf script to retrieve the IP, and generate the dnsmasq config entry.
 
It suddenly doesn't work anymore ! ! !

Now, when I type: nslookup publicip.local I get:

*** No internal type for both IPv4 and IPv6 Addresses (A+AAAA) records available for publicip.local ***

What's going on here? I used to work fine. Has there been any change to dnsmasq, that has disabled this option?

I don't know exactly when it stopped working - I also got a new public address yesterday (from a different range) and of course I have updated Merlin to newest.
 
Well, I assume it is, why wouldn't it be (and how can I check?
Also, I don't see any dnsmasq errors in log.
 
Thanks, I learned a new thing.
Yes, hardware acceleration is turned on.
For others: go to 'Tools', look for 'HW Acceleration'.
If it's turned on, change your script to:

interface-name=publicip.local,vlan2

and it works again.
 

Similar threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top