Community discussions

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

Join two scripts

Wed Apr 27, 2022 4:02 pm

I have 2 scripts and they work very well, but I have realized that I can make a single script to do both jobs, but I don't know how to do it, these are the two scripts:
: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
:local newIP [/ip address get [find interface="ETHERT 1"] address]
:set newIP [:pick $newIP 0 [:find $newIP "/" -1]]
:if ($newIP != $currentIP) do={
    /file
    :delay 5s
    :local filecontent [get $filenameIP contents]
    :local newfilecontent "\r\n************************************************************\r\nNEW IP: $newIP\t$now\r\n$filecontent"
    set $filenameIP contents=$newfilecontent
    :set currentIP $newIP;
}

:global currentIP
:local newIP [/ip address get [find interface="ETHERT 1"] address]
:set newIP [:pick $newIP 0 [:find $newIP "/" -1]]
:if ($currentIP != $newIP) do={
    /tool e-mail send to="XXXXXXX@hotmail.com" subject=[/system identity get name] start-tls=yes \
        body=" THIS IS THE NEW IP:  $newIP"
    set currentIP $newIP
}
Thanks for your help friends.




EL DONCITO.
Last edited by eldoncito2019 on Wed Apr 27, 2022 5:15 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12001
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Join two scripts

Wed Apr 27, 2022 4:17 pm

really you need help to copy-and-paste two "e-mail" lines from second script on same place to first script?
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Join two scripts

Wed Apr 27, 2022 4:24 pm

Thanks for responding friend, I need a single script to send me the new IP address to my email and at the same time register the new IP in the file.txt. You understand me?.




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

Re: Join two scripts

Wed Apr 27, 2022 7:19 pm

simply copy & paste
    /tool e-mail send to="XXXXXXX@hotmail.com" subject=[/system identity get name] start-tls=yes \
        body=" THIS IS THE NEW IP:  $newIP"
before :set currentIP $newIP;
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Join two scripts

Wed Apr 27, 2022 10:34 pm

Thanks for your answer friend REXTENDED, but I just realized that if I have both scripts running at the same time, the one that sends the new IP to the mail does work and the one that registers the new IP in the file.txt does not work, what will happen there?.





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

Re: Join two scripts

Wed Apr 27, 2022 10:40 pm

because both use same :global variable currentIP
:global currentIP
and the script can not check if the IP is changed, if the variable is :local.
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Join two scripts

Wed Apr 27, 2022 10:46 pm

I don't understand, do you mean that the same global variable currentIP cannot be held by both scripts? and that one of the 2 scripts must have a global variable such as :global storedIP





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

Re: Join two scripts

Wed Apr 27, 2022 11:05 pm

REXTENDED, how would you solve that?







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

Re: Join two scripts

Thu Apr 28, 2022 12:30 am

you can change all the occurencies of the variable name on one of the two script
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Join two scripts

Thu Apr 28, 2022 12:45 am

ok, what if the local variable newIP is placed instead as :global newIP?





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

Re: Join two scripts

Thu Apr 28, 2022 1:08 am

As I have already told you another time, I am not answering assumptions.
Obviously, the more variables become global, the more scripts interact with each other.

It 's the case that you study at least a little, on basic things I can not be a teacher.

But if some users want to do it, PLEASE...

Who is online

Users browsing this forum: eworm, malks and 26 guests