script send email as user login into routerboard

Hello, i want a script that send me a email if user login into routerboard.
Is that possible and where shall a run it?

Hi,
thanks again for Karma for other script (http://forum.mikrotik.com/t/send-email-with-all-address-in-an-address-list/76548/2)

About your request:

Login by?

You can see if one user (like admin) are logged from winbox with one command like:

:local HowManyAdminAreOnlineViaWinbox value=[:len [/user active find where name=admin and via=“winbox”]]

NEVER FORGET THE QUOTES “” between the word after “via=”

HowManyAdminAreOnlineViaWinbox can be “0” if no one user called “admin” are online (by winbox)
HowManyAdminAreOnlineViaWinbox can be “1” if only one user called “admin” are online (by winbox)
HowManyAdminAreOnlineViaWinbox can be “2” or more if more than one user called “admin” are online (by winbox, multiple winbox windows)

You can use also the keyword “local”, “console”, “telnet”, “ssh”, “api” and “web”,
and you can know accordingly what method for access are used.

“New Terminal” inside WinBox, for example, are counted as second access via “local”

I hope this info suggest the right soluctions :wink:

For spimply have a list of user logged on that moment

:if ([:len [/user active find]] > 0) do {
 :local tmpMessage value="";
 :foreach tmpArrayItem in [/user active find] do {
  :set $tmpMessage value=($tmpMessage.[/user active get value-name=name $tmpArrayItem]." logged from ".[/user active get value-name=when $tmpArrayItem]." via ".[/user active get value-name=via $tmpArrayItem]."\r\n");
 };
 :log info message=("Logged user(s) ".[/system clock get date]." ".[/system clock get time]."\r\n".$tmpMessage);
};

By.

Suggest after suggest… I made the script for you… :slight_smile:

Schedule that script every appropriate time, like 10 seconds?

Syntax is for RouterOS 6.7+

:log info message=("Start Check Logged Users");
:local tmpAllTheUsersLogged value=[/user active find];
:if ([:len $tmpAllTheUsersLogged] > 0) do={
 :local tmpMessage value="";
 :foreach tmpArrayItem in=$tmpAllTheUsersLogged do={
  :set $tmpMessage value=($tmpMessage.[/user active get value-name=name $tmpArrayItem]." logged from ".[/user active get value-name=when $tmpArrayItem]." via ".[/user active get value-name=via $tmpArrayItem]."\r\n");
 };
 :set $tmpMessage value=("Logged user(s) ".[/system clock get date]." ".[/system clock get time]."\r\n".$tmpMessage);
 :if ($cluEmailSent != "true") do={
  :log info message=$tmpMessage;
  /tool e-mail send from="clu.script@routerboard.local" to="your@mail.it" subject=("Logged user(s) report of ".[/system clock get date]." ".[/system clock get time]) body=$tmpMessage;
  :global cluEmailSent value="true";
 };
} else={
 :global cluEmailSent value="false";
};
:log info message=("Check Logged Users End");

(Notice: a blank line on log after all user logged are normal, because \r\n at the end of each line)

You also must schedule this at startup of routerboard:

:global cluEmailSent value="false";

It prevents multiple sends of same email.
The email are sent again, when first logged users go to 0, and increase again.

Hello Rextented, thx for reply and effort you deserve the karma
The script works like a charm

@ rextended. Can you make a script if someone TRY to login on the routerboard?

Hmmm… wait two mins…

You can use this:

:put [/log find where message~"login failure"]

It give all ID of message in log rerarding failed login attempts,
store at boot this value on one :global variable and on scheduler check every x minutes if
previous global variable != [/log find where message~“login failure”]
if different send one email and update
and do not forget to update again the global variable with new value…

Some hint for you… :wink:

Thanks, can you give me the fully script to email please

Have you find one personal assistant?

Ahahahaha!!! :laughing:


Ok, wait, I must write the script…

Thanks Rextended, its verry appriciated your help.

I made the script for you… :slight_smile:

Schedule that script every appropriate time, like 10 seconds?

Syntax is for RouterOS 6.7+

:log info message=("Start Check Failed Login");
:local currentLoginFailureArray value=[/log find where message~"login failure"];
:if ($currentLoginFailureArray != $previosLoginFailureArray) do={
 :local tmpMessage value="";
 :foreach tmpArrayItem in=$currentLoginFailureArray do={
  :set $tmpMessage value=($tmpMessage.[/log get value-name=time $tmpArrayItem]." ".[/log get value-name=topic $tmpArrayItem]." ".[/log get value-name=message $tmpArrayItem]."\r\n");
 };
 :set $tmpMessage value=("Login Failure(s) ".[/system clock get date]." ".[/system clock get time]."\r\n".$tmpMessage);
 :log info message=$tmpMessage;
 /tool e-mail send from="clu.script@routerboard.local" to="your@mail.it" subject=("Logged user(s) report of ".[/system clock get date]." ".[/system clock get time]) body=$tmpMessage;
 :global previosLoginFailureArray value=$currentLoginFailureArray;
};
:log info message=("Check Failed Login End");

(Notice: a blank line on log after all log entry are normal, because \r\n at the end of each line)

You also must schedule this at startup of routerboard:

:global previosLoginFailureArray value=[/log find where message~"login failure"];

Mmm.. look familiar… :slight_smile:

I’ts working perfect, thanks a lot rextended.
I give you karma if i can but must waiting

I just need to know if it working to you.

Thanks.

It’s working fine thank you verry match

can someone confirm that the script above work in 6.26 … “script if someone TRY to login on the routerboard”

For me does not work in 6.26

I have try and working fine with ROS 6.26

I use the logging function to email when a user logs into the routerboard.

Setup email, in the tools menu, the under logging create a new action, type email.

Then, add a new logging rule.
Topic: Account, Action ‘Your new emailing action’

Now try logging in from winbox and you should receive an email.

this works for succefull login. i want to be emailed for failure login!

thanx i just did it. i just have to select in topics “critical” and put to prefix “login failure”.
very nice without script!



hi,your script is working fine, but when I run it 3-4 times, my 750upr2 get 100% of cpu occupation then crash and reboot.

Anyone got this?