Community discussions

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

Help with this script

Mon Apr 04, 2022 6:02 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help with this script

Mon Apr 04, 2022 6:55 pm

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
}
Last edited by rextended on Mon Apr 04, 2022 7:36 pm, edited 2 times in total.
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Help with this script

Mon Apr 04, 2022 7:07 pm

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help with this script

Mon Apr 04, 2022 7:17 pm

a) In file just write the current IP, what could be going wrong? Thanks for your help.
b) The interface where the internet arrives is ETHER 1
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
}
Last edited by rextended on Mon Apr 04, 2022 7:36 pm, edited 2 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help with this script

Mon Apr 04, 2022 7:21 pm

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}
Last edited by rextended on Mon Apr 04, 2022 7:37 pm, edited 2 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help with this script

Mon Apr 04, 2022 7:27 pm

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}
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Help with this script

Mon Apr 04, 2022 7:39 pm

In file just write the current IP, what could be going wrong?



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

Re: Help with this script

Mon Apr 04, 2022 8:12 pm

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
Last edited by eldoncito2019 on Mon Apr 04, 2022 8:13 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help with this script

Mon Apr 04, 2022 8:13 pm

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]
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Help with this script

Tue Apr 05, 2022 3:19 am

Friend, I already put the line that he told me and now how do I know where the file is saved?




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

Re: Help with this script

Tue Apr 05, 2022 2:53 pm

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
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Help with this script

Tue Apr 05, 2022 4:08 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help with this script

Tue Apr 05, 2022 4:10 pm

that part... never...
wait 8 min
Last edited by rextended on Tue Apr 05, 2022 4:21 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help with this script  [SOLVED]

Tue Apr 05, 2022 4:18 pm

Same as post #2, but added date and time, like:

out_file_example code

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
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Help with this script

Tue Apr 05, 2022 5:23 pm

the file comes out blank friend




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

Re: Help with this script

Tue Apr 05, 2022 5:26 pm

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.....
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Help with this script

Tue Apr 05, 2022 5:49 pm

I have version 6.49.4, I'm going to download it to your version to see


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

Re: Help with this script

Tue Apr 05, 2022 6:15 pm

Solved friend Rextended




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

Re: Help with this script

Tue Apr 05, 2022 6:50 pm

I'm happy for you,
thanks.
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Help with this script

Wed Apr 06, 2022 4:18 pm

Thanks for your help friend Rextended


EL DONCITO

Who is online

Users browsing this forum: Ahrefs [Bot] and 22 guests