Register file. txt uptime

hello guys, you can make a .txt file to record the time the routerboard was started for the first time with the following data:

Date: Apr/17/2022
Time: 13:10:10
Uptime: 1d 00:09:20

Thanks for your help.




EL DONCITO:

Try this:

:local newdate [/system clock get date]
:local newtime [/system clock get time]
:local newuptime [/system resource get uptime]
:local filename "infosystem.txt"

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

:delay 5s
:local filecontent [get $filename contents]
:local newfilecontent "Info: $newdate\t$newtime\t$newuptime\r\n$filecontent"
set $filename contents=$newfilecontent

File content:

Info: apr/19/2022	18:06:50	5d04:52:13
Info: apr/19/2022	18:06:40	5d04:52:03
Info: apr/19/2022	18:06:25	5d04:51:48
Info: apr/19/2022	18:06:02	5d04:51:26

BR.

Thanks friend, but it is not what I need, what I need is that according to the uptime, you can give me the information at what time the routerboard reboot.




EL DONCITO.

You will have to get in your WAN interface → Status, the date in the “Last Link UP Time” field in a global variable.

BR.

@diamuxin
Why all that?
And why you post the script than I have already maked for @eldoncito2019?


@eldoncito2019
Again with “started for the fisrt time”?
How many topic you want open for the same identical thing?
http://forum.mikrotik.com/t/a-little-help-please/157117/1
http://forum.mikrotik.com/t/thanks-to-rextended/157224/1
http://forum.mikrotik.com/t/thanks-to-rextended/157224/4

I need your help friends.




EL DONCITO.

It will keep the same errors on the other scripts:
do not write the right time until the time is not syncronized,
but until you do not want use (S)NTP, you do not know when the time is sincronized
and if you put a delay of 10 min or less for be sure the time is synced, surely you also want calculate the uptime / time set differencies for write in the file the right time.
Nothing familiar?
Everytime the same things…


And, yes, I help you again, but not now, I must go home,
I write later.
If I forgot this, write here.
Thanks.


Some hint for myself later:
Check if http://worldclockapi.com/api/json/cet/now is reachable,
when reachable read http://worldclockapi.com/api/json/cet/now
the datetime is after currentDateTime":" and before ","utcOffset
split date and time at the T
remove TimeZone after time
write the string in the file with uptime
done.

EL DONCITO: what are your time zone name? (not +08:00, but name like CET, Central Europe Time)

if I can go to: http://worldclockapi.com/api/json/cet/now
I can read; http://worldclockapi.com/api/json/cet/now
and my time zone is: america/ caracas and the GMT offset: -04:00





EL DONCITO.

Venezuela Standard Time / VST
http://worldclockapi.com/api/json/vst/now

http://forum.mikrotik.com/t/fetch-capable-of-following-redirects/151723/7

=

Step 1)
Check if the URL is available, and display what is get from URL:

/file remove [find where name="testfetch.txt"]
{
    # seconds of timeout, after that time stop try get the time from url
    :local timeout 120
    :local jobid [:execute file=testfetch.txt script="/tool fetch url=\"http://worldclockapi.com/api/json/vst/now\""]
    :put "\r\nWaiting the end of process for file testfetch.txt to be ready, max $timeout seconds..."
    :global Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={
        :set Gltesec ($Gltesec + 1)
        :delay 1s
        :put "waiting... $Gltesec"
    }
    :put "Done. Elapsed Seconds: $Gltesec\r\n"
    :if ([:len [/file find where name="testfetch.txt"]] = 1) do={
        :local filecontent [/file get [/file find where name="testfetch.txt"] contents]
        :put "Result of Fetch:\r\n****************************\r\n$filecontent\r\n****************************"
        :if ([:len [/file find where name="now"]] = 1) do={
            :local filecontent [/file get [/file find where name="now"] contents]
            :put "\r\nFile content:\r\n****************************\r\n$filecontent\r\n****************************"
        } else={
            :put "\r\nURL not reached."
        }
    } else={
        :put "Generic error, Test file not created."
    }
}

Step 2)
Elaborate file content and get date and time:

/file remove [find where name="testfetch.txt"]
{
    # seconds of timeout, after that time stop try get the time from url
    :local timeout 120
    :local jobid [:execute file=testfetch.txt script="/tool fetch url=\"http://worldclockapi.com/api/json/vst/now\""]
    :put "\r\nWaiting the end of process for file testfetch.txt to be ready, max $timeout seconds..."
    :local Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={
        :set Gltesec ($Gltesec + 1)
        :delay 1s
        :put "waiting... $Gltesec"
    }
    :put "Done. Elapsed Seconds: $Gltesec\r\n"
    :if ([:len [/file find where name="testfetch.txt"]] = 1) do={
        :local filecontent [/file get [/file find where name="testfetch.txt"] contents]
        :put "Result of Fetch:\r\n****************************\r\n$filecontent\r\n****************************"
        :if ([:len [/file find where name="now"]] = 1) do={
            :local filecontent [/file get [/file find where name="now"] contents]
            :put "\r\nFile content:\r\n****************************\r\n$filecontent\r\n****************************"
            :local dstart ([:find $filecontent "currentDateTime\":\"" -1] + 18)
            :local year  [:pick $filecontent $dstart ($dstart + 4)]
            :local month [:pick $filecontent ($dstart + 5 ) ($dstart + 7)]
            :local day   [:pick $filecontent ($dstart + 8 ) ($dstart + 10)]
            :local hm    [:pick $filecontent ($dstart + 11 ) ($dstart + 16)]
            :put "\r\nTime readed from the URL: $year-$month-$day $hm\r\n"
        } else={
            :put "\r\nURL not reached."
        }
    } else={
        :put "Generic error, Test file not created."
    }
}

Step 3)
Add file creation

/file remove [find where name="testfetch.txt"]
{
    :local filename "lastupurl.txt"
    # seconds of timeout, after that time stop try get the time from url
    :local timeout 120
    :local jobid [:execute file=testfetch.txt script="/tool fetch url=\"http://worldclockapi.com/api/json/vst/now\""]
    :put "\r\nWaiting the end of process for file testfetch.txt to be ready, max $timeout seconds..."
    :local Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={
        :set Gltesec ($Gltesec + 1)
        :delay 1s
        :put "waiting... $Gltesec"
    }
    :put "Done. Elapsed Seconds: $Gltesec\r\n"
    :if ([:len [/file find where name="testfetch.txt"]] = 1) do={
        :local filecontent [/file get [/file find where name="testfetch.txt"] contents]
        :put "Result of Fetch:\r\n****************************\r\n$filecontent\r\n****************************"
        :if ([:len [/file find where name="now"]] = 1) do={
            :local filecontent [/file get [/file find where name="now"] contents]
            :put "\r\nFile content:\r\n****************************\r\n$filecontent\r\n****************************"
            :local dstart ([:find $filecontent "currentDateTime\":\"" -1] + 18)
            :local year  [:pick $filecontent $dstart ($dstart + 4)]
            :local month [:pick $filecontent ($dstart + 5 ) ($dstart + 7)]
            :local day   [:pick $filecontent ($dstart + 8 ) ($dstart + 10)]
            :local hm    [:pick $filecontent ($dstart + 11 ) ($dstart + 16)]
            :put "\r\nTime readed from the URL: $year-$month-$day $hm\r\n"
            /file
            :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
            :if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 5s; set $filename contents=""}
            :delay 5s
            :local newfilecontent "Date: $year-$month-$day\r\nTime: $hm\r\nUptime: $[/system resource get uptime]"
            set $filename contents=$newfilecontent
        } else={
            :put "\r\nURL not reached."
        }
    } else={
        :put "Generic error, Test file not created."
    }
    /file remove [find where name="testfetch.txt"]
    /file remove [find where name="now"]
}

Special EL DONCITO version
Date on abbreviated venezuelan montn name/ day / year and Time on AM/PM format
http://forum.mikrotik.com/t/can-it-be-done-with-a-script/157158/10
http://forum.mikrotik.com/t/one-last-little-help-please/157270/1


{
    :local filename "lastupurl.txt"
    :local timeout 60
    :local url "http://worldtimeapi.org/api/timezone/America/Caracas.txt"
    /file
    remove [find where name="fetchtest.txt"]
    remove [find where name="fetchnow.txt"]
    :local jobid [:execute file=fetchtest.txt script="/tool fetch url=\"$url\" dst-path=fetchnow.txt"]
    :local Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={:set Gltesec ($Gltesec + 1); :delay 1s}
    :if ([:len [find where name="fetchnow.txt"]] = 1) do={
        :local filecontent [get [find where name="fetchnow.txt"] contents]
        :local dstart ([:find $filecontent "datetime: " -1] + 10)
        :local year  [:pick $filecontent $dstart ($dstart + 4)]
        :local month [:pick $filecontent ($dstart + 5 ) ($dstart + 7)]
        :local day   [:pick $filecontent ($dstart + 8 ) ($dstart + 10)]
        :local hms   [:pick $filecontent ($dstart + 11 ) ($dstart + 19)]
        :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
        :if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 5s; set $filename contents=""}
        :delay 5s
            :global arrMonths {"01"="ENE";"02"="FEB";"03"="MAR";"04"="ABR";"05"="MAY";"06"="JUN";\
                "07"="JUL";"08"="AGO";"09"="SEP";"10"="OCT";"11"="NOV";"12"="DIC"}
            :local mhours [:tonum [:pick $hms 0 2]]
            :local minsec [:pick $hms 2 8]
            :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"}
        set $filename contents="Date: $($arrMonths->$month)/$day/$year\r\nTime: $mhours$minsec $msymbl\r\n\
            Uptime: $[/system resource get uptime]"
    }
    remove [find where name="fetchtest.txt"]
    remove [find where name="fetchnow.txt"]
}

Date: ABR/20/2022
Time: 04:49:23 AM
Uptime: 00:02:26

International version
Date on ISO format year-month-day and timezone UTC

{
    :local filename "lastupurl.txt"
    :local timeout 60
    :local url "http://worldtimeapi.org/api/timezone/Etc/UTC.txt"
    /file
    remove [find where name="fetchtest.txt"]
    remove [find where name="fetchnow.txt"]
    :local jobid [:execute file=fetchtest.txt script="/tool fetch url=\"$url\" dst-path=fetchnow.txt"]
    :local Gltesec 0
    :while (([:len [/sys script job find where .id=$jobid]] = 1) && ($Gltesec < $timeout)) do={:set Gltesec ($Gltesec + 1); :delay 1s}
    :if ([:len [find where name="fetchnow.txt"]] = 1) do={
        :local filecontent [get [find where name="fetchnow.txt"] contents]
        :local dstart ([:find $filecontent "datetime: " -1] + 10)
        :local year  [:pick $filecontent $dstart ($dstart + 4)]
        :local month [:pick $filecontent ($dstart + 5 ) ($dstart + 7)]
        :local day   [:pick $filecontent ($dstart + 8 ) ($dstart + 10)]
        :local hms   [:pick $filecontent ($dstart + 11 ) ($dstart + 19)]
        :if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
        :if ([:len [find where name=$filename]] = 0) do={print file="$filename"; :delay 5s; set $filename contents=""}
        :delay 5s
        set $filename contents="Date: $year-$month-$day\r\nTime: $hms\r\nUptime: $[/system resource get uptime]"
    }
    remove [find where name="fetchtest.txt"]
    remove [find where name="fetchnow.txt"]
}

Date: 2022-04-20
Time: 08:49:23
Uptime: 00:02:26