Last active
March 15, 2025 17:59
-
-
Save SmartFinn/3f860d11470c701e67e508d09ba65321 to your computer and use it in GitHub Desktop.
A script to send an email when a user logged in the MikroTik router #RouterOS #MikroTik
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
# This script sends an email when a new user logged in the router | |
# | |
# Required permissions: | |
# | |
# [x] read [x] write | |
# [x] policy [x] test | |
# | |
# written by Sergei Eremenko (https://github.com/SmartFinn) | |
:global TokensOfLoggedUsers | |
:local emailAddress "[email protected]" | |
:local token | |
:local tokenList | |
:local newSession | |
:local msgBody "" | |
/user active { | |
:foreach session in=[find via!="api"] do={ | |
:set token ([get $session name]."-".[get $session via]."-".[get $session when]); | |
:set tokenList ($token.",".$tokenList); | |
:set newSession true; | |
:foreach savedToken in=$TokensOfLoggedUsers do={ | |
:if ($savedToken = $token) do={ | |
:set newSession false; | |
} | |
}; | |
:if ($newSession) do={ | |
:set msgBody ($msgBody.[get $session name]." logged in from ".[get $session address]); | |
:set msgBody ($msgBody." via ".[get $session via]." at ".[get $session when]."\r\n"); | |
} | |
} | |
}; | |
if ($msgBody != "") do={ | |
:local msgSubj ("New user(s) logged in to ".[/system identity get name]); | |
:set msgBody ($msgBody."\r\n"."At ".[/system clock get time]); | |
:set msgBody ($msgBody.", ".[/user active print count-only]." management session(s)"); | |
:set msgBody ($msgBody." is active on ".[/system identity get name]); | |
/tool e-mail send to=$emailAddress subject=$msgSubj body=$msgBody; | |
}; | |
:set TokensOfLoggedUsers [:toarray $tokenList]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
find via!="api" via!="console"
it is best ( Do't send alert on New terminal)