Created
May 26, 2023 14:54
-
-
Save fxlv/6126b2571a41fc1ca433ace57b46cd56 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# pushover notification script for mikrotik routers | |
# scp it to the router and then run: | |
# | |
# /file/remove pushover.rsc | |
# /system script remove pushover | |
# /system/script/add name=pushover source=[/file get pushover.rsc contents] | |
# | |
# enable it to run at startup with this command | |
# /system/scheduler/add name=pushover on-event=pushover start-time=startup interval=0 | |
# | |
# | |
# set your token and user id as global variables using another script that runs at startup, or, | |
# alternatively, hardcode them here | |
# :global pushovertoken "super secret token here" | |
# :global pushoveruser "super secret user key here" | |
# | |
# as it runs at startup ,it might be that not all interfaces are up yet, so let's not quite rush into it and wait for a second | |
:delay delay-time=1 | |
# time in seconds | |
:global sleeptime 1 | |
# wait for 5 minutes before giving up | |
:global maxretries (60*5) | |
:global retries 0 | |
# use the global variables | |
:global pushovertoken | |
:global pushoveruser | |
:local pushoverurl "https://api.pushover.net/1/messages.json" | |
:local extipurl "http://ip.bgp.lv/robo.php" | |
# get the external IP by querying a remote server, | |
# in this case http://ip.bgps.lv/robo.php, however you could use any other; | |
# it will provide the IP address in the response | |
# and this way we will know for sure, that the router is connected to internet | |
:global fetchresult 0 | |
:while ($fetchresult = 0) do={ | |
:set retries ($retries + 1) | |
:do { | |
:set fetchresult [/tool/fetch url=$extipurl as-value output=user]; | |
:log error "fetchresult: $fetchresult" | |
} on-error= { | |
:log error "Failed to connect to the server in order to fetch external IP; retries: $retries/$maxretries" | |
:delay delay-time=$sleeptime | |
} | |
if ($retries > $maxretries) do={ | |
:error "Failed to fetch external IP after $retries retries, Pushover notification script failed" | |
} | |
} | |
if ($fetchresult != 0) do={ | |
if (($fetchresult->"status") = "finished") do={ | |
:log debug "External IP acquired successfully" | |
:global extip [($fetchresult->"data")] | |
:local name [/system/identity/get name] | |
:local title "$name is online" | |
:local message "External IP: $extip. Router is online after $retries retries." | |
:local payload "token=$pushovertoken&user=$pushoveruser&title=$title&message=$message" | |
# https://help.mikrotik.com/docs/display/ROS/Fetch | |
/tool fetch mode=https url=$pushoverurl http-method=post keep-result=no http-data=$payload | |
} | |
} else { | |
:log error "Fetching of external IP failed, Pushover notification script failed" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment