Community discussions

MikroTik App
 
User avatar
mves
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 91
Joined: Tue Jan 11, 2011 8:15 pm
Location: Serbia

Creating text file help

Wed Apr 02, 2014 5:47 pm

Hello
I need a bit help with script.
I need a script that will run through firewall and picking entries with comments that contains # in it and creating text file with name that goes [ENTRY COMMENT - IP - OFF/ON]. Off/on means status of entry, disabled=no or disabled=yes.

Any help or direction on this will be much appreciated.

Thanks in advance
 
User avatar
mves
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 91
Joined: Tue Jan 11, 2011 8:15 pm
Location: Serbia

Re: Creating text file help

Fri Apr 04, 2014 8:13 pm

I managed to assemble script but i still having some bit troubles. I actually need to get status inside file and there i got a bit trouble. I can't edit file. I can't actually find right file... this is a test script. Error is in locating created file.
:foreach i in=[/ip firewall filter find] do={
:local test ([/ip firewall filter get $i comment]~"#")
:if ([/ip firewall filter get $i comment]~"#" = true) do={
:local comment [/ip firewall filter get $i comment]
:local IP [/ip firewall filter get $i src-address]
:local status [/ip firewall filter get $i disabled]
:if ($status = false) do={:set status disabled}
:if ($status = true) do={:set status enabled}
:log warning ($comment." - ".$IP)
:local myfile ($comment." - ".$IP)
:local file ($myfile . ".txt");
/file print file=$myfile
:foreach j in=[/file find] do={
:local filename [/file get $j name]
:local num [/file get $j number]
:if ($filename = $myfile) do={/file set numbers=$num contents=$status}
}}}
error is in :local num [/file get $j number]
How can I get number of file to edit it? I tried even with name of a file but same problem. It have to work on ROS 5 and 6.
 
User avatar
skot
Long time Member
Long time Member
Posts: 584
Joined: Wed Nov 30, 2011 3:05 am

Re: Creating text file help

Fri Apr 04, 2014 9:45 pm

You're on the right track. What would work better is using [find] instead of targeting the item number. Then you are only looping through the entries that match your query. The following code loops through all firewall filter rules that contain # in the comment. The delay is there because for some reason after creating a new file, it is not immediately accessible for writing.
/ip firewall filter
:foreach i in=[find comment~"#"] do={
  :local filename ([get $i comment] . " - " . [get $i src-address])
  :local status [get $i disabled]
  :if ($status = true) do={:set status "disabled"} else={:set status "enabled"}
  /file print file="$filename"
  :delay 1s
  /file set [find name~"$filename"] contents=$status
}
And... this is just a bonus for verifying the script worked. It finds the files that contain # in the name and displays their contents. Copy/paste into the terminal.
/file
:foreach i in=[find name~"#"] do={
  :put ([get $i name] . " - " .[get $i contents])
}
 
User avatar
mves
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 91
Joined: Tue Jan 11, 2011 8:15 pm
Location: Serbia

Re: Creating text file help

Fri Apr 04, 2014 11:27 pm

Now, this works perfectly :D

Who is online

Users browsing this forum: No registered users and 6 guests