What's new

Getting WAN IP in a script

  • 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!

david3

Occasional Visitor
I used to use Tomato firmware, and I have scripts that use the WAN IP.

I could get it like this:

Code:
#!/bin/sh
WANIP=$(nvram get wan_ipaddr)
echo $WANIP

Now I've updated my router (an Asus RT-N66U) to use Asuswrt-Merlin firmware, and I guess it doesn't work the same way, since the above code isn't giving me my WAN IP.

What's the best way to get the WAN IP for Asuswrt-Merlin?

The above does get an IP, but it's not my current IP (maybe an old one?)

Thanks.
 
Both wan_ipaddr and wan0_ipaddr work for me.

Edit: There's also wan1_ipaddr for dual WAN support.
 
Last edited:
The nvram command also works for me.
As an alternative though I just did this and it also works (assuming eth0 is your WAN port);
ifconfig eth0 | grep "inet addr" | sed -r 's/.*r:(.*) B.*/\1/'


Sent from my iPhone using Tapatalk
 
Use wan0_ipaddr
wan_ipaddr is a gui only construct that reflects the address in effect the last time you accessed a page that needed the wan address.
 
Thanks, it looks like wan0_ipaddr is working.

Code:
#!/bin/sh
WANIP=$(nvram get wan0_ipaddr)
echo $WANIP

wan_ipaddr returns something different. Maybe the previous or an old WAN IP address.
 

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