Suggestions for getting WAN IP from interface with multiple IPs

Hi

I’m trying to get some DDNS updater scripts working, and have come across a common issue. Where there is code such as this:
:local currentIP [/ip address get [find interface=“$inetinterface” disabled=no] address]
This doesn’t work for me, as my interface has two IP addresses (one for the WAN, another for the cable modem management interface), so find interface is getting an array.

Is there a common way of getting whichever of these addresses is the public routable IP instead of the internal one?

Cheers!

Assuming you have configured at least one DNS server on your RouterBOARD, you may use this:

local currWANAddress [toip [resolve myip.opendns.com server=resolver1.opendns.com]]

It will give you the WAN address of your current default gateway (least distance 0.0.0.0/0 route). Source
(You can use the provision and dyndns modules of RouterSCRIPTS as well - they’re pretty reliable)

@Frostbyte
Problem is that he try to update a DNS system, so you assume he has already a working DDNS, some he does not have.

@naxxfish
Have you looked at Cloud (IP->Cloud) on the Router? It’s a free dynamic DNS updater from Miktrotik so you do not need other script.

Try this to get public IP (This will only get outside IP, so if you are behind another NAT, you do not get the public IP)

:put [/ip address get [find interface=[/ip route get [find dst-address=0.0.0.0/0] vrf-interface]] address]

or this using the cloud function. (It will get the public ip, even if its behind other NAT)

:put [/ip cloud get public-address]

Hey @Jotne, I would suggest to read the question and answer again.

problem: update DDNS
there was nothing said about DNS, but one can only assume that it’s working…

and there was also no mentioning of VRF routing, so that won’t work either

AND
if I run “:put [toip [resolve myip.opendns.com server=resolver1.opendns.com]]” it does exactly what requester is asking… → Frostbyte give the right solution IMHO

Ok I read it again,

Suggestions for getting WAN IP from interface with multiple IPs



I’m trying to get some DDNS updater scripts working,

DDNS = Dynamic DNS https://en.wikipedia.org/wiki/Dynamic_DNS

From what I do read. He need to get the public IP, the one that its routed to the internt, so that he can make a script to update for example no-ip.com site with his IP, so that he can reach his Router using a public DNS service form other side of the internet.

If you ask to resolve myip.opendns.com, you already has updated the opendns.com with your IP. He has not done that yet.

Ok, I understand now.

myip.opendns.com is a special feature of opendns servers to give the requester’s ip back. That’s a then a way to determine the routeable ip.

Does not work, at leas for me for some reason
This works
http://whatismyip.com

But as I posted above, you can use the built in cloud function to get the IP.

When you resolve myip.opendns.com you also need to supply server=resolver1.opendns.com to the command.
sebastia is correct that this is a special feature which replies back to you, with your WAN IP address.
This feature can be used regardless of which Dynamic DNS service you’re using. We assume that this has been dealt with, as it’s out of scope for this topic.
This feature does not update your Dynamic DNS service, as that requires credentials to do so (explained right below).

The script must then do a fetch (HTTP POST Request) to the corresponding API of the Dynamic DNS (i.e. NoIP, DynDNS) service that is being used, with the:

  • Username
  • Password
  • Hostname
  • WAN IP address (that we just determined with said resolve command).

The problem was, as the user described it, that he had no way to determine his WAN/Internet address - and the excerpt which I supplied does exactly that.