Last active
January 10, 2024 13:21
-
-
Save rossigee/19d2d622c8395c7db99704fd40a68730 to your computer and use it in GitHub Desktop.
RouterOS raise HTTP event when dynamic IP address changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:global previousIP 1.2.3.4 | |
:global ipAddressCheckURL "http://ifconfig.me/ip" | |
# or... :global ipAddressCheckURL "http://icanhazip.com" | |
:global ipChangedEventURL "https://webhook.site/12345678-9610-4f57-9bfe-a3c5e2551d26" | |
/system/script/add name=reportDynamicIPChanged source={ | |
:global ipAddressCheckURL | |
:global ipChangedEventURL | |
:local result [/tool fetch url=$ipAddressCheckURL mode=http as-value output=user]; | |
:if ($result->"status" = "finished") do={ | |
:local currentIP ($result->"data"); | |
:local previousIP [/system script environment get [find name="previousIP"] value]; | |
:local systemID [/system identity get name]; | |
:local eventID [/rndstr length=32]; | |
:local eventTime [/system clock get time as-string]; | |
:local formattedTime ([/system clock get date as-string] . "T" . $eventTime . "Z"); | |
:if ($currentIP != $previousIP) do={ | |
/system script environment set [find name="previousIP"] value=$currentIP; | |
:local jsonPayload ("{\"specversion\": \"1.0\", \"type\": \"tech.golder.infra.ipaddressupdates\", \"source\": \"/" . $systemID . "/script/reportDynamicIPChanged\", \"id\": \"" . $eventID . "\", \"time\": \"" . $formattedTime . "\", \"subject\": \"IP Address Update\", \"datacontenttype\": \"application/json\", \"data\": {\"name\": \"" . $systemID . "\", \"ipv4\": \"" . $currentIP . "\", \"ipv6\": \"\"}, \"dataschema\": \"https://golder.tech/schemas/ipaddressupdates\"}"); | |
/tool fetch url=$ipChangedEventURL http-method=post http-header-field="Content-Type: application/json" http-data=($jsonPayload); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment