Community discussions

MikroTik App
 
longschao
newbie
Topic Author
Posts: 26
Joined: Wed Oct 03, 2018 2:40 pm

get date of file and calculate need help

Fri Mar 24, 2023 5:11 am

Hello, this is my first topic in this forum, and my mother tongue isn't English, may be I'm looks like not polite enough,trust me, I'm not mean it.

Here is my problem, my ISP use CGNAT on IPv4, my router is CCR1009, it get a 10.0.0.0/8 reserved ip address on PPPoE out interface, and a 2408:8000::/20 IPv6 address, right now, those address are 10.70.163.179, and 2408:8207:254a:7150:62e7:d3ff:fe04:2780. so when I need link devices in my room, I configured a openvpn over a DYNV6 domain, to IPv6 address of my router. But last morning, I found that IPv6 address was over 19 days not change, my ISP should change it each 3 days. that cause my router didn't update DDNS by script in nearly 19 days, and DYNV6 paused DNS resolution of my domain. my script run each 5 minutes, check and update my domain when the IPv6 address change before, so I add a counter in script, to force update my domain in each 10 days. But I think if my router shutdown, like power failure, the counter will be lost, and if my router still get a same IPv6 address as same as last time, that will cause my domain not get update in a long time, maybe a other 19 days. So I'm thinking, there is a flie download when update my DYNV6 domain, what about get date of this file, and calculate it, that will know how many days since last update. But I've no idea how to get the date and calculate.
The files download when update DYNV6, they named same, the file name will be "update?hostname=MYDOMAIN.dynv6.net&token=1a2b3c4d5e6f".

My added counter script of update DYNV6 below:
{
:local dynv6addr
:local currentv6addr
:local dynv6domain "MYDOMAIN.dynv6.net"
:local dynv6token "1a2b3c4d5e6f"
:local dynv6url
:global dynv6holdcounter
:set $currentv6addr [/ipv6 address get [/ipv6 address find interface=pppoe-out-Unicom from-pool=unicom-pool-v6] address]
:set $currentv6addr [:pick $currentv6addr 0 [:find $currentv6addr "/"]]
:set $dynv6addr [:resolve domain-name=MYDOMAIN.dynv6.net server=2620:fe::fe]
:set $dynv6url "http://dynv6.com/api/update?hostname=$dynv6domain&token=$dynv6token&ipv6=$currentv6addr"
:if ([:typeof $currentv6addr]="ip6" && :get[$currentv6addr]!="::1") do={
	:if (:get$currentv6addr!=:get $dynv6addr || :get[$dynv6holdcounter]="2880") do={
	:set $dynv6holdcounter 0;
	/file remove "update?hostname=MYDOMAIN.dynv6.net&token=1a2b3c4d5e6f";
	/tool fetch url=($dynv6url) mode=http;
	/log info message=("IPv6 address of dynv6 updated to ".$currentv6addr);
	} else={
	:set $dynv6holdcounter ($dynv6holdcounter+1);
	}
} else={
/log info message="bad ipv6 address for dynv6.";
:set $dynv6holdcounter 0;
}
}
thanks a lot.
Last edited by longschao on Mon Mar 27, 2023 7:19 am, edited 1 time in total.
 
longschao
newbie
Topic Author
Posts: 26
Joined: Wed Oct 03, 2018 2:40 pm

Re: get date of file and calculate need help

Fri Mar 24, 2023 9:31 am

I find out how to get date of file, and I wrote another script for calculate how many days did'n update DYNV6, run once per day. I ignored leap year problem this time, but there is some thing wrong in my script, I need some help.
{
:global dynv6holdday
:local currentdate
:local currentday
:local currentmonth
:local currentyear
:local currentdaynumber
:local updatedate
:local updateday
:local updatemonth
:local updateyear
:local updatedaynumber
:local arrPreMonDays {an=0;eb=31;ar=59;pr=90;ay=120;un=151;ul=181;ug=212;ep=243;ct=273;ov=304;ec=334}
:set $currentdate [/system/clock/get date]
:set $currentmonth [:pick $currentdate 1 3]
:set $currentday [:tonum [:pick $currentdate 4 6]]
:set $currentyear [:tonum [:pick $currentdate 7 11]]
:set $updatedate [/file get "update?hostname=MYDOMAIN.dynv6.net&token=1a2b3c4d5e6f"  value-name=creation-time ]
:set $updatemonth [:pick $updatedate 1 3]
:set $updateday [:tonum [:pick $updatedate 4 6]]
:set $updateyear [:tonum [:pick $updatedate 7 11]]
:set $currentdaynumber ($currentday + ($arrPreMonDays->$currentmonth))
:set $updatedaynumber ($updateday + ($arrPreMonDays->$updatemonth))
/log info message=($currentdate", "$updatedate".")
:if ($currentyear > $updateyear) do={
	:set $currentdaynumber ($currentdaynumber + ($currentyear - $updateyear) * 365);
}
:set $dynv6holdday ($currentdaynumber - $updatedaynumber)
/log info message=("dynv6 already " $dynv6holdday "days not update.")
}
Thank you for any help.
Last edited by longschao on Mon Mar 27, 2023 7:18 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get date of file and calculate need help

Fri Mar 24, 2023 2:35 pm

Do not lost time in useless frills.
You need to do something every 10 days?
Schedule someting that run every 10 days: done...

(and when you copy someone script is nice to cite the source...)


And about the update script before, why wear uselessly the flash? Has a limited write count...


But if you insist on that way...
How to calculate time difference between current RouterOS date and time and timezone to one internal file (or just between two dates)
In the example is checked the date of the "skins" folder. On device that have flash the name must be "flash/skins"
:global datetime2epoch do={
    :local dtime [:tostr $1]
    /system clock
    :local cyear [get date] ; :if ($cyear ~ "....-..-..") do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
    :if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime "$dtime 00:00:00"}
    :if ([:len $dtime] = 15) do={:set dtime "$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]"}
    :if ([:len $dtime] = 14) do={:set dtime "$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]"}
    :if ([:len $dtime] =  8) do={:set dtime "$[get date] $dtime"}
    :if ([:tostr $1] = "") do={:set dtime ("$[get date] $[get time]")}
    :local vdate [:pick $dtime 0 [:find $dtime " " -1]]
    :local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
    :local vgmt  [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
    :if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
    :local arrm  [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local vdoff [:toarray "0,4,5,7,8,10"]
    :local MM    [:pick $vdate ($vdoff->2) ($vdoff->3)]
    :local M     [:tonum $MM]
    :if ($vdate ~ ".../../....") do={
        :set vdoff [:toarray "7,11,1,3,4,6"]
        :set M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
        :if ($M>12) do={:set M ($M - 12)}
    }
    :local yyyy  [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
    :local totd  ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
    :return      (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}

{
:local unixnow   [$datetime2epoch]
:local timestamp [$datetime2epoch [/file get skins creation-time]]
:local diff      ($unixnow - $timestamp)
:local days      (($unixnow - $timestamp) / 86400)
:put "Time differences between the two dates: $diff (~$days Days)"
}
Last edited by rextended on Fri May 19, 2023 3:17 pm, edited 6 times in total.
 
longschao
newbie
Topic Author
Posts: 26
Joined: Wed Oct 03, 2018 2:40 pm

Re: get date of file and calculate need help

Mon Mar 27, 2023 4:11 am

Do not lost time in useless frills.
You need to do something every 10 days?
Schedule someting that run every 10 days: done...

(and when you copy someone script is nice to cite the source...)


And about the update script before, why wear uselessly the flash? Has a limited write count...


But if you insist on that way...
How to calculate time difference between current RouterOS date and time and timezone to one internal file (or just between two dates)
In the example is checked the date of the "skins" folder. On device that have flash the name must be "flash/skins"
Thank you.
I want to calculate the time between twice update, because I heard about DYNV6.com required user update threir domains when IP address changed, not allowed many times update for the same IP address. So I'm thinking if I schedule to 10 days update once, when power failure, maybe my router will update my domain in a row. and I don't like DYNV6 disable my service.
Let me make my script better, I think I find a way, thank you for your scripts again.

And I'm not understand about write flash part, Is the Conuter in my script do that?
 
longschao
newbie
Topic Author
Posts: 26
Joined: Wed Oct 03, 2018 2:40 pm

Re: get date of file and calculate need help

Mon Mar 27, 2023 5:26 am

I changed my script to this:

Script A run every 5 minutes, if the global function dynv6holdday over 14, will force update my domain.
Script B run once in one day, set function dynv6holdday to calculate how many days not update DYNv6, base on when DYNV6 update, it's will download a web page file.
And I'm still trying to understand rextended's script.
# script A:
{
/delay 30
:local dynv6addr
:local currentv6addr
:local dynv6domain "MYDOMAIN.dynv6.net"
:local dynv6token "1a2b3c4d5e"
:local dynv6url "http://dynv6.com/api/"
:local dynv6filename
:set $currentv6addr [/ipv6 address get [find interface=pppoe-out-Unicom from-pool=unicom-pool-v6] address]
:set $currentv6addr [:pick $currentv6addr 0 [:find $currentv6addr "/"]]
:set $dynv6addr [:resolve domain-name=arizuka.dynv6.net server=2620:fe::fe]
:set $dynv6filename "update?hostname=$dynv6domain&token=$dynv6token&ipv6=$currentv6addr"
/log info message=($dynv6url)
:if ([:typeof $currentv6addr]!="nil" && $currentv6addr!="::1") do={
	:if ($currentv6addr!=$dynv6addr || $dynv6holdday>="14") do={
	/file remove [/file get [find contents="addresses updated"] name];
	/tool fetch url=("$dynv6url.$dynv6filename") mode=http;
	/log info message=("IPv6 address of dynv6 updated to ".$currentv6addr);
	}
} else={/log info message="bad IPv6 address for dynv6.";}
}

# Scrpit B:
{
:global dynv6holdday
:local currentdate
:local currentday
:local currentmonth
:local currentyear
:local currentdaynumber
:local updatedate
:local updateday
:local updatemonth
:local updateyear
:local updatedaynumber
:local arrPreMonDays
:set $arrPreMonDays {an=0;eb=31;ar=59;pr=90;ay=120;un=151;ul=181;ug=212;ep=243;ct=273;ov=304;ec=334}
:set $currentdate [/system/clock/get date]
:set $currentmonth [:pick $currentdate 1 3]
:set $currentday [:tonum [:pick $currentdate 4 6]]
:set $currentyear [:tonum [:pick $currentdate 7 11]]
:set $updatedate [/file get [find contents="addresses updated"] value-name=creation-time]
:set $updatemonth [:pick $updatedate 1 3]
:set $updateday [:tonum [:pick $updatedate 4 6]]
:set $updateyear [:tonum [:pick $updatedate 7 11]]
:set $currentdaynumber ($currentday+($arrPreMonDays->$currentmonth))
:set $updatedaynumber ($updateday+($arrPreMonDays->$updatemonth))
:if ($currentyear > $updateyear) do={
	:set $currentdaynumber ($currentdaynumber + ($currentyear - $updateyear) * 365);
}
:set $dynv6holdday ($currentdaynumber - $updatedaynumber)
/log info message=("dynv6 not update in : ".$dynv6holdday." days.")
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get date of file and calculate need help

Mon Mar 27, 2023 10:14 am

If what only matter is the IP.... save inside RouterOS the IP and update only if after a reboot the current IP is different to the saved one...
You can use "unused" layer7 rules for store variables than must keeped between reboots...
/ip firewall layer7-protocol
add name="dyndns_ip" regexp="0.0.0.0"
add name="dyndns_ipv6" regexp="::"

set [find where name="dyndns_ip"] regexp="127.45.68.79"
set [find where name="dyndns_ipv6"] regexp="dead:beef:46::44"

:put [get [find where name="dyndns_ip"] regexp]
:put [get [find where name="dyndns_ipv6"] regexp]
 
longschao
newbie
Topic Author
Posts: 26
Joined: Wed Oct 03, 2018 2:40 pm

Re: get date of file and calculate need help

Mon Mar 27, 2023 11:00 am

If what only matter is the IP.... save inside RouterOS the IP and update only if after a reboot the current IP is different to the saved one...
You can use "unused" layer7 rules for store variables than must keeped between reboots...
That maybe a great idea, but I've no idea, how to get last update time by use layer7.
I'd like figure your script out, although I'm not fully understand it. what is "$1"? I can find out where is it come from.
Thank you.
Last edited by longschao on Mon Mar 27, 2023 11:24 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get date of file and calculate need help

Mon Mar 27, 2023 11:20 am

What do you care about the last date?
Simply if the saved IP is different from the current one, update the saved IP with the new one and update the dyndns...
Why you going to complicate things?...
 
longschao
newbie
Topic Author
Posts: 26
Joined: Wed Oct 03, 2018 2:40 pm

Re: get date of file and calculate need help

Mon Mar 27, 2023 11:29 am

What do you care about the last date?
Simply if the saved IP is different from the current one, update the saved IP with the new one and update the dyndns...
Why you going to complicate things?...
Because at first, I found mydomain stop gave me any resolution, that day, I noticed my ISP didn't changed my IPv6 address over 19 days. so my router didn't update DDNS as long as that.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get date of file and calculate need help

Mon Mar 27, 2023 11:30 am

Let's me understand, your dyndns service unbind the IPs if for some reason have passed more than xx days without update???
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get date of file and calculate need help

Mon Mar 27, 2023 11:32 am

I'd like figure your script out, although I'm not fully understand it. what is "$1"? I can find out where is it come from.
the
:global datetime2epoch do={
[…]
}
is a function, must not be touched, and the $1 mean the 1st parameter passed to the function

is used on 2nd block:
{
:local unixnow [$datetime2epoch]
[…]
:put "Time differences between the two dates: $diff (~$days Days)"
}
Last edited by rextended on Thu May 11, 2023 3:18 am, edited 1 time in total.
 
longschao
newbie
Topic Author
Posts: 26
Joined: Wed Oct 03, 2018 2:40 pm

Re: get date of file and calculate need help

Mon Mar 27, 2023 11:49 am

Let's me understand, your dyndns service unbind the IPs if for some reason have passed more than xx days without update???
My ISP gave me a PPPoE internet line, without fixed IP, normally, this PPPoE session redial every time, I got new IP addresses include IPv4 and IPv6. But, last time, I found my PPPoE client didn't redial in 19 days. Usually, my ISP should force the client offline once per 3 to 4 days, to redial PPPoE, but not happend. So I got a problem with my domain.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get date of file and calculate need help

Mon Mar 27, 2023 11:53 am

Ok, split the problem on two...
First is the usual dyndns need to upgrade the IP only if change,
the other is to force redial the pppoe after X times stil up?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: get date of file and calculate need help

Mon Mar 27, 2023 11:58 am

For detect, use the correct name "pppoe-out1" and wanted time. Changing the name (with the same) force pppoe to reconnect.
:local pppoename      "pppoe-out1"
:local redialinterval 2d00:00:00
/interface pppoe-client
:if (([monitor $pppoename once as-value]->"uptime") > $redialinterval) do={
    set $pppoename name=$pppoename
}
 
longschao
newbie
Topic Author
Posts: 26
Joined: Wed Oct 03, 2018 2:40 pm

Re: get date of file and calculate need help

Mon Mar 27, 2023 12:12 pm

Ok, split the problem on two...
First is the usual dyndns need to upgrade the IP only if change,
the other is to force redial the pppoe after X times stil up?
In my situation, my router didn't do anything, because my IPv6 didn't change at all during this time, and DYNV6 stop my domain DNS resolution, it comes nothing result. I've tried to force my router last friday, make it redial the PPPoE in several times, and it got a same IPv6 address. So I got a idea, if the IPv6 address not change in another 14 days, force my router to upgrade DYNV6 once.
 
GerishSonya
just joined
Posts: 2
Joined: Tue Mar 28, 2023 10:20 am

Re: get date of file and calculate need help

Thu Apr 06, 2023 3:03 pm

facing an issue with your ISP's use of CGNAT on IPv4 and your router's IPv6 address not changing as frequently as it should. While you have added a counter in your script to force updates, you're concerned about losing the counter in case of a power failure. You're considering calculating the days since the last update by getting the date of the file downloaded during the DYNV6 update. However, you're unsure of how to get the date and perform the calculation.

Who is online

Users browsing this forum: No registered users and 16 guests