Dear friend, I have the following script and I don’t know why it doesn’t copy the currentIP in the file. txt, I also want that every time the IP changes, I do it in the file. txt, as a record, that is, do not overwrite what has already been written. Thank you for your cooperation.
:global r "NEW IP";
:global currentIP;
:local newIP [/ip address get [find interface="ETHERT 1"] address];
:if ($newIP != $currentIP) do={
:local newIP [/ip address get [find interface="ETHERT 1"] address];
:local data1 (" CURRENT IP: " . $currentIP . " NEW IP: " . $newIP);
/file print file=$r;
/file set $r contents=$data1;
:set $currentIP $newIP;
}
EL DONCITO.
Ready
:global filenameIP "iphistory.txt"
/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 storedIP
:global interfaceIP "ether1"
:if ([:len $storedIP] = 0) do={:set storedIP 0.0.0.0/0}
:global currentIP [/ip address get [find where interface="$interfaceIP"] address]
:if ($currentIP != $storedIP) do={
/file
:delay 4s
:local filecontent [get $filenameIP contents]
:local newfilecontent "$currentIP\r\n$filecontent"
set $filenameIP contents=$newfilecontent
:set storedIP $currentIP
}
Commented
:global filenameIP "iphistory.txt"
/file
# is a Flash or NAND ... If is a Flash, save the file inside flash disk or when rebooting the file is lost.
# Just remove this line for save on RAM on devices with Flash memory, but lost on reboot.
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filenameIP "flash/$filenameIP"}
# if the file not exist, create it and empty first
:if ([:len [find where name=$filenameIP]] = 0) do={print file="$filenameIP"; :delay 5s; set $filenameIP contents=""}
:global storedIP
:global interfaceIP "ether1"
# if not exist the variable, create it
:if ([:len $storedIP] = 0) do={:set storedIP 0.0.0.0/0}
:global currentIP [/ip address get [find where interface="$interfaceIP"] address]
:if ($currentIP != $storedIP) do={
/file
:delay 4s
# read file
:local filecontent [get $filenameIP contents]
# add the latest IP at the start, because file lenght > ~63K are truncated and lost
:local newfilecontent "$currentIP\r\n$filecontent"
# save back the new file content
set $filenameIP contents=$newfilecontent
:set storedIP $currentIP
}
In file just write the current IP, what could be going wrong? Thanks for your help.
The interface where the internet arrives is ETHER 1
EL DONCITO
a) Only one IP? No problem (see under)
b) just replace interface name “ether1” with “ETHER 1”
Script variation: Only last IP on file
:global filenameIP "lastip.txt"
/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 storedIP
:global interfaceIP "ether1"
:if ([:len $storedIP] = 0) do={:set storedIP 0.0.0.0/0}
:global currentIP [/ip address get [find where interface="$interfaceIP"] address]
:if ($currentIP != $storedIP) do={
/file
:delay 4s
:local newfilecontent "$currentIP"
set $filenameIP contents=$newfilecontent
:set storedIP $currentIP
}
Or shorter version:
:global filenameIP "lastip.txt"
:global interfaceIP "ether1"
/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="0.0.0.0/0"}
:global currentIP [/ip address get [find where interface="$interfaceIP"] address]
:delay 4s
:global storedIP [get $filenameIP contents]
:if ($currentIP != $storedIP) do={set $filenameIP contents=$currentIP}
Without submask /xx:
:global filenameIP "lastip.txt"
:global interfaceIP "ether1"
/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 4s; set $filenameIP contents="0.0.0.0"}
:global currentIP [/ip address get [find where interface="$interfaceIP"] address]
:set currentIP [:pick $currentIP 0 [:find $currentIP "/" -1]]
:delay 4s
:global storedIP [get $filenameIP contents]
:if ($currentIP != $storedIP) do={set $filenameIP contents=$currentIP}
In file just write the current IP, what could be going wrong?
EL DONCITO
Sorry friend, as it is, it’s fine, I just want you to tell me how to save it in the internal memory of my RB, so that the file is not lost when the RB is restarted
EL DONCITO
I have already predicted that need on first reply 
if you do not want subnet mask /xx
add this line:
:set currentIP [:pick $currentIP 0 [:find $currentIP “/” -1]]
just after
:global currentIP [/ip address get [find where interface=“$interfaceIP”] address]
Friend, I already put the line that he told me and now how do I know where the file is saved?
EL DONCITO.
The file name is inside the variable $filenameIP,
you can open winbox for download it on file section, using drag&drop
or
send the file by e-mail (but email section must be already configured and working)
/tool e-mail send to="xxx@xxx.xx" subject="Last IP" file=$filenameIP
i never suggest to use ftp
Very grateful friend Rextended and how would I put this part in the script?
:local day ([:pick [/system clock get date] 4 6]);
:local mon ([:pick [/system clock get date] 0 3]);
:local year ([:pick [/system clock get date] 7 11]);
:local date "$day-$mon-$year";
:local time (:put [/system clock get time]);
to know the exact time and date when the IP changes
EL DONCITO.
that part… never…
wait 8 min
Same as post #2, but added date and time, like:
127.5.44.1/24 2022/04/05 15:19:30
:local interfaceIP "ether1"
:local filenameIP "iphistory.txt"
:global storedIP
:if ([:len $storedIP] = 0) do={:set storedIP 0.0.0.0/0}
:local 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"}
/system clock
:local ndate [get date]
:local ntime [get time]
:local now "$[:pick $ndate 7 11]-$($arrMonths->[:pick $ndate 0 3])-$[:pick $ndate 4 6] $[get time]"
/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=""}
:local currentIP [/ip address get [find where interface="$interfaceIP"] address]
:if ($currentIP != $storedIP) do={
/file
:delay 4s
:local filecontent [get $filenameIP contents]
:local newfilecontent "$currentIP\t$now\r\n$filecontent"
set $filenameIP contents=$newfilecontent
:set storedIP $currentIP
}
if you copy & paste inside excel, you can have 2 colums with separated IP / Date values
the file comes out blank friend
EL DONCITO.
just re-tested (I use RouterOS 6.48.6) and work…
Check if you copy-paste rightly
You set the right interface name for your case, instead of ether1?
P.S.: is a good practice to not change default interface name…
I have version 6.49.4, I’m going to download it to your version to see
EL DONCITO
Solved friend Rextended
EL DONCITO
I’m happy for you,
thanks.
Thanks for your help friend Rextended
EL DONCITO