Community discussions

MikroTik App
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

where is the error?

Wed Oct 16, 2024 7:58 pm

There is a working script for Router OS 6 in the Scheduler that reports the admin login to the router:
:local lstime ([/system scheduler get value-name=next-run [find name="login"]] - [/system scheduler get value-name=interval [find name="login"]] * 2);
:foreach i in=[/log find message~"user admin logged in from" && time>=$lstime] do={

:log info "admin in"}
It works fine.

I want to make it work in POS 7. Since in the POS 7 log the time is indicated with the date (a different time format), I decided to use [:tonsec]
I rewrote the script this way:
:local SchedName "check_login"
:local userName "admin"

:local lstime ([:tonsec [/system scheduler get value-name=next-run [find name=$SchedName]]] - [:tonsec [/system scheduler get value-name=interval [find name=$SchedName]]] *2); 
:foreach i in=[/log find message~"user $userName logged in from" && [:tonsec time]>=$lstime] do={

:log info "admin in"}
And it doesn't work. I can't figure out where the error is?
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: where is the error?

Thu Oct 17, 2024 9:03 am

Didn't change anything, just converted all the time intervals into seconds? ...
 
msatter
Forum Guru
Forum Guru
Posts: 2936
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: where is the error?

Thu Oct 17, 2024 10:42 am

Debugging is done in steps. I fail at the first step:
:put ([/system scheduler get value-name=next-run [find name~"Load"]])  
invalid internal item number
Is there still a next-run available in 7.x!?
 
erlinden
Forum Guru
Forum Guru
Posts: 2466
Joined: Wed Jun 12, 2013 1:59 pm
Location: Netherlands

Re: where is the error?

Thu Oct 17, 2024 10:48 am

I might misread (should use reading glasses), do you use ~ instead of = ?
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: where is the error?

Thu Oct 17, 2024 11:05 am

:put ([/system scheduler get value-name=next-run [find name~"Load"]])  
invalid internal item number
Do you have a scheduler job named "Load"? next-run is in /system scheduler ROS7

It's better to be more specific, or maybe you have several tasks in the Scheduler that include “Load” in the name
:put ([/system scheduler get value-name=next-run [find name="Load"]])  
Last edited by Sertik on Thu Oct 17, 2024 11:15 am, edited 2 times in total.
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: where is the error?

Thu Oct 17, 2024 11:06 am

I might misread (should use reading glasses), do you use ~ instead of = ?
Yes, I use ~ in
:foreach i in=[/log find message~"user $userName logged in from" && [:tonsec time]>=$lstime] do={
but who forbids me to do this?
Last edited by Sertik on Thu Oct 17, 2024 11:17 am, edited 1 time in total.
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: where is the error?

Thu Oct 17, 2024 11:07 am

ROS7 does not find syntax errors in my script. There's a timing error...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12438
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: where is the error?

Thu Oct 17, 2024 3:07 pm

As usual, is missing what you try to do....

At RouterOS start memorize in one global variable
:put [/user get admin last-logged-in]
And check if is changed on scheduler.
When is changed, you know the exact time the user log in.

Works on both v6/v7
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: where is the error?

Thu Oct 17, 2024 3:12 pm

That's it, I figured it out. This is how it works in ROS 7
[:tonsec] not needed
:local SchedName "check_login"
:local userName "admin"

:local Stime ([pick [/system scheduler get value-name=next-run [find name=$SchedName]] 11 19] - [/system scheduler get value-name=interval [find name=$SchedName]] *2); 
:foreach i in=[/log find message~"user $userName logged in from" && time>=$Stime] do={
:log info "admin in"}
in 7, last-logget-in and /shheduler next run work with the full date and time, and log time is only time. In version 6 /system scheduler, next-run only worked over time.
Last edited by Sertik on Fri Oct 18, 2024 9:10 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12438
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: where is the error?

Thu Oct 17, 2024 3:18 pm

Still useless do all that instead of simply [/user get admin last-logged-in]
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 489
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: where is the error?

Thu Oct 17, 2024 3:25 pm

I need it for another reason. Thank you, Teacher.

Also /user get admin last-logged-in shows the time of the last previous, and not the current login of the administrator to the router.

Who is online

Users browsing this forum: benonet and 8 guests