Community discussions

MikroTik App
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Date in script

Fri Apr 15, 2022 4:08 pm

Guys, I have the following script thanks to REXTENDED and it works very well, it records in file every time the IP address changes, but when the routerboard is rebooted and its time has not yet been synchronized, it records in file that IP address with that time How would I go about solving that problem.
:global interfaceIP "ETHERT 1"
:global filenameIP "iphistory.txt"

:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
{
             :local data [/system clock get date]
             :local itime  [/system clock get time]
             :local hours  [:pick $itime 0 2]
             :local minsec [:pick $itime 2 8]
             :local mhours [:tonum $hours]
             :local msymbl "AM"
             :if ($mhours > 11) do={:set msymbl "PM"}
             :set mhours ($mhours % 12)
             :if ($mhours = 0) do={:set mhours 12}
             :if ($mhours < 10) do={:set mhours "0$mhours"}
             :local mtime "$mhours$minsec $msymbl"
:global now "time:$mtime   date: $[:pick $data 4 6]-$($arrMonths->[:pick $data 0 3])-$[:pick $data 7 11]"
}
/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filenameIP "flash/$filenameIP"}
:if ([:len [find where name=$filenameIP]] = 0) do={print file="$filenameIP"; :delay 5s; set $filenameIP contents=""}

:global currentIP
:if ([:len $currentIP] = 0) do={:set currentIP 0.0.0.0/0}
:local newIP [/ip address get [find interface="$interfaceIP"] address]
:set newIP [:pick $newIP 0 [:find $newIP "/" -1]]
:if ($newIP != $currentIP) do={
    /file
    :delay 5s
    :local filecontent [get $filenameIP contents]
    :local newfilecontent "new IP:$newIP\t$now\r\n$filecontent"
    set $filenameIP contents=$newfilecontent
    :set currentIP $newIP;
}

here we have an example:

new IP:186.95.13.112	time:18:57:20 PM   date: 06-04-2022
new IP:186.95.45.220	time:08:54:30 AM   date: 15-04-2022
new IP:190.78.186.59	time:07:34:20 PM   date: 14-04-2022
new IP:186.95.84.150	time:05:49:30 PM   date: 14-04-2022
new IP:186.95.45.220	time:05:35:51 PM   date: 14-04-2022
new IP:190.72.12.81	time:05:34:23 PM   date: 14-04-2022


EL DONCITO:
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Date in script

Fri Apr 15, 2022 4:49 pm

You have already requested that,
and I have already wroted about that
viewtopic.php?p=924616#p924559
viewtopic.php?p=924616#p924606
and already the soluction: use NTP client and wait until the time is not syncronized....
:do { :delay 1s } while=([:typeof [/system ntp client get last-update-before]] != "time")
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Date in script

Fri Apr 15, 2022 5:19 pm

FWIW I think V7 remove "last-update-time" . I don't know the context of @rextended code... but you may have to do something like this if V7
:do { :delay 1s } while=([/system ntp client get status] != "synchronized")

That's presuming the NTP Client is enabled/setup, and /ip/cloud is NOT what's being used.

But even a simple check to see if the clock's year is 1970 could work. As could using comparing :timestamp to number of week since 1970, e.g.
([:timestamp] < 2728w)
if you didn't use NTP & on V7.
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Date in script

Fri Apr 15, 2022 6:02 pm

I use version 6.49.4 and I don't have SNTP enabled, because in this version the time is given by the mikrotik without the need for SNTP.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Date in script

Fri Apr 15, 2022 6:07 pm

And as I already wrote, you don't know if the time is synchronized or not that way.

@Amm0
When the RouterBOARD is rebooted, it uses the last time sync stored inside, it does not restart from 1970,

log code

sntp change time Apr/15/2022 17:10:07 => Apr/15/2022 17:12:23
unless MikroTik has not removed this thing in later versions,
and I already know that the script is not for v7 (or for v6 with separate ntp package installed)
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Date in script

Fri Apr 15, 2022 7:27 pm

And if we put something like this:


:global filenamedate "datehistoryII.txt"

/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filenamedate "flash/$filenamedate"}
:if ([:len [find where name=$filenamedate]] = 0) do={print file="$filenamedate"; :delay 5s; set $filenamedate contents=""}

:global currentdate [/system clock get date]
{
:local newdate [/sytem clock get date]
:if ($currentdate != $newdate) do={
    /file
    :delay 4s
    :local filecontent [get $filenamedate contents]
    :local newfilecontent "$currentdate\r\n$filecontent"
    set $filenamedate contents=$newfilecontent
    :set $newdate $currentdate
}
}


It's just an idea





EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Date in script

Fri Apr 15, 2022 8:08 pm

why after some nanoseconds "{" you can read different "date" ?
Is near impossbile than the 2 variables have 2 different dates inside....
:global currentdate [/system clock get date]
{
:local newdate [/sytem clock get date]

I have already provided to you the "soluction", and until someone do not provide any VALID alternative soluction, I do not comment other supposition

Sorry.
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Date in script

Sat Apr 16, 2022 2:18 am

badie help me please







EL DONCITO,

Who is online

Users browsing this forum: navigator and 22 guests