Community discussions

MikroTik App
 
tomislav91
Member Candidate
Member Candidate
Topic Author
Posts: 123
Joined: Fri May 26, 2017 12:47 pm

script dont create file?

Wed Oct 14, 2020 1:30 pm

Hi, in the thread "tunnel troubleshot" in the General tab, i posted some issue with tunneling, and i want to get some script to have monitoring ipsec installed sa and append to existing file, but when I run script manualy via GUI, nothing happend, where is mistake?
 :set time [/system clock get time]
:local file [/ip ipsec installed-sa print file=ipsec append]

:local contents [/file get $file contents]
:set contents ($contents . "\n" . $time)
/file set $file contents=$contents
}
When i run just
/ip ipsec installed-sa print file=ipsec append
it is OK, and file have been created. What is problem with the script?
 
Lelik200
just joined
Posts: 22
Joined: Fri Jul 24, 2020 1:48 pm

Re: script dont create file?

Tue Oct 20, 2020 12:04 pm

Most likely you are trying to get more than 4096 bytes into a variable:
:local contents [/file get $file contents]

viewtopic.php?t=127093
It is limit not to file - it is limit for variable size...
If you write text variable to file - you can write maximum 4096 bytes.

But, ":execute" have't "append" option! And you can't use it for add line to your's log file.
And "/system clock print" have't "append" option..
As an option, it is passible to use global variable - "/system script environment print" have "append" option!

And so we have:
:global TimeVar
:local SAfileName "ipsec.txt"

:set TimeVar [/system clock get time]
/ip ipsec installed-sa print file=$SAfileName append
/system script environment print file=$SAfileName append where name="TimeVar"
:set TimeVar

ipsec.txt:
# oct/20/2020 11:45:44 by RouterOS 6.47.4
# software id = 0Q17-AD9J
#
Flags: H - hw-aead, A - AH, E - ESP 
# oct/20/2020 11:45:44 by RouterOS 6.47.4
# software id = 0Q17-AD9J
#
 # NAME               VALUE                                                    
 0 TimeVar            11:45:44                                                 
# oct/20/2020 11:45:48 by RouterOS 6.47.4
# software id = 0Q17-AD9J
#
Flags: H - hw-aead, A - AH, E - ESP 
# oct/20/2020 11:45:48 by RouterOS 6.47.4
# software id = 0Q17-AD9J
#
 # NAME               VALUE                                                    
 0 TimeVar            11:45:48                                                 

On my router, every time I use "..print .. file .. append" the timestamp and RoOS information is added to the file. If you have the same thing, then you do not need to use a global variable for your task at all.
 
tomislav91
Member Candidate
Member Candidate
Topic Author
Posts: 123
Joined: Fri May 26, 2017 12:47 pm

Re: script dont create file?

Wed Oct 21, 2020 11:07 am

Most likely you are trying to get more than 4096 bytes into a variable:
:local contents [/file get $file contents]

viewtopic.php?t=127093
It is limit not to file - it is limit for variable size...
If you write text variable to file - you can write maximum 4096 bytes.

But, ":execute" have't "append" option! And you can't use it for add line to your's log file.
And "/system clock print" have't "append" option..
As an option, it is passible to use global variable - "/system script environment print" have "append" option!

And so we have:
:global TimeVar
:local SAfileName "ipsec.txt"

:set TimeVar [/system clock get time]
/ip ipsec installed-sa print file=$SAfileName append
/system script environment print file=$SAfileName append where name="TimeVar"
:set TimeVar

ipsec.txt:
# oct/20/2020 11:45:44 by RouterOS 6.47.4
# software id = 0Q17-AD9J
#
Flags: H - hw-aead, A - AH, E - ESP 
# oct/20/2020 11:45:44 by RouterOS 6.47.4
# software id = 0Q17-AD9J
#
 # NAME               VALUE                                                    
 0 TimeVar            11:45:44                                                 
# oct/20/2020 11:45:48 by RouterOS 6.47.4
# software id = 0Q17-AD9J
#
Flags: H - hw-aead, A - AH, E - ESP 
# oct/20/2020 11:45:48 by RouterOS 6.47.4
# software id = 0Q17-AD9J
#
 # NAME               VALUE                                                    
 0 TimeVar            11:45:48                                                 

On my router, every time I use "..print .. file .. append" the timestamp and RoOS information is added to the file. If you have the same thing, then you do not need to use a global variable for your task at all.

thats it. THANKS MAN!

Who is online

Users browsing this forum: Google [Bot], sutrus and 32 guests