Skip to content

Instantly share code, notes, and snippets.

@liamcottle
Created December 29, 2021 14:15
Show Gist options
  • Save liamcottle/fed28c33542bc5755ce3044518abf505 to your computer and use it in GitHub Desktop.
Save liamcottle/fed28c33542bc5755ce3044518abf505 to your computer and use it in GitHub Desktop.
Retrieve the public IP address of a specific Mikrotik RouterBoard interface.
:global getPublicIP do={
# tag for temporary configurations
:local tag "get-public-ip-via-$interface"
# service that provides public ip address in http response
:local lookupProtocol "https"
:local lookupHostname "api.ipify.org"
# add temporary address list for lookup service
/ip firewall address-list add list=$tag address=$lookupHostname comment=$tag timeout=15s
:delay 3;
# configure temporary route to lookup service via a specific interface
:foreach p in=[/ip firewall address-list find comment=$lookupHostname dynamic=yes] do={
:local addr [/ip firewall address-list get $p address];
/ip route add gateway=$gateway dst-address=$addr comment=$tag
}
# fetch http response from lookup service
:local result [/tool fetch url="$lookupProtocol://$lookupHostname" as-value output=user]
# remove temporary address list
/ip firewall address-list remove [find where comment=$tag]
# remove temporary gateway route
/ip route remove [find where comment=$tag]
# return ip from lookup service response data
:return ($result->"data")
}
# get public ip addresses
:local iplte [$getPublicIP interface="lte1" gateway="192.168.8.1"]
:local ipfibre [$getPublicIP interface="Fibre-PPPoE" gateway="Fibre-PPPoE"]
# log ips
:log info "Fibre IP: $ipfibre"
:log info "LTE IP: $iplte"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment