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:
    14
    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 Very Senior Member

    Joined:
    May 19, 2018
    Messages:
    1,506
    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"
    
     
  3. Jack Yaz

    Jack Yaz Part of the Furniture

    Joined:
    Apr 20, 2017
    Messages:
    2,469
    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:
    14
    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 Very Senior Member

    Joined:
    May 19, 2018
    Messages:
    1,506
    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:
    14
    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:
    31,537
    Location:
    Canada
    Code:
    getrealip.sh
    
     
    L&LD and martinr like this.
  8. Poul Bak

    Poul Bak Occasional Visitor

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

    RMerlin Super Moderator

    Joined:
    Apr 14, 2012
    Messages:
    31,537
    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.