1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
Dismiss Notice

Welcome To SNBForums

SNBForums is a community for anyone who wants to learn about or discuss the latest in wireless routers, network storage and the ins and outs of building and maintaining a small network.

If you'd like to post a question, simply register and have at it!

While you're at it, please check out SmallNetBuilder for product reviews and our famous Router Charts, Ranker and plenty more!

TIP: How to get your public ipv4 address programmatical

Discussion in 'Asuswrt-Merlin' started by Poul Bak, Oct 17, 2019.

  1. Poul Bak

    Poul Bak Occasional Visitor

    Joined:
    Dec 2, 2017
    Messages:
    21
    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.
     
    L&LD and dave14305 like this.
  2. dave14305

    dave14305 Part of the Furniture

    Joined:
    May 19, 2018
    Messages:
    3,281
    Location:
    USA
    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"
    
     
    Poul Bak likes this.
  3. Jack Yaz

    Jack Yaz Part of the Furniture

    Joined:
    Apr 20, 2017
    Messages:
    3,799
    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
     
  4. Poul Bak

    Poul Bak Occasional Visitor

    Joined:
    Dec 2, 2017
    Messages:
    21
    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: Oct 18, 2019
  5. dave14305

    dave14305 Part of the Furniture

    Joined:
    May 19, 2018
    Messages:
    3,281
    Location:
    USA
    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.
     
  6. Poul Bak

    Poul Bak Occasional Visitor

    Joined:
    Dec 2, 2017
    Messages:
    21
    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
     
  7. RMerlin

    RMerlin Super Moderator

    Joined:
    Apr 14, 2012
    Messages:
    33,242
    Location:
    Canada
    Code:
    getrealip.sh
    
     
  8. Poul Bak

    Poul Bak Occasional Visitor

    Joined:
    Dec 2, 2017
    Messages:
    21
    Believe it not, a few people actually use something called Windows :D
     
  9. RMerlin

    RMerlin Super Moderator

    Joined:
    Apr 14, 2012
    Messages:
    33,242
    Location:
    Canada
    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.
     
    L&LD and dave14305 like this.
  10. Poul Bak

    Poul Bak Occasional Visitor

    Joined:
    Dec 2, 2017
    Messages:
    21
    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.
     
  11. dave14305

    dave14305 Part of the Furniture

    Joined:
    May 19, 2018
    Messages:
    3,281
    Location:
    USA
    Is your WAN interface still eth0?
     
  12. Poul Bak

    Poul Bak Occasional Visitor

    Joined:
    Dec 2, 2017
    Messages:
    21
    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.
     
  13. ColinTaylor

    ColinTaylor Part of the Furniture

    Joined:
    Mar 31, 2014
    Messages:
    12,024
    Location:
    UK
    If you change hardware acceleration from disabled to enabled the interface changes from eth0 to vlan2 (depending on the specific model of router ;)).

    nvram get wan0_ifname


    N.B See post #2.
     
    Poul Bak likes this.
  14. Poul Bak

    Poul Bak Occasional Visitor

    Joined:
    Dec 2, 2017
    Messages:
    21
    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.
     
    dave14305 likes this.
  15. ColinTaylor

    ColinTaylor Part of the Furniture

    Joined:
    Mar 31, 2014
    Messages:
    12,024
    Location:
    UK
    Or use Dave's script in post #2 which takes care of any changes automatically.
     
    dave14305 likes this.