Community discussions

 
User avatar
SteveG
just joined
Topic Author
Posts: 2
Joined: Thu Dec 13, 2018 11:51 pm
Location: Belgium
Contact:

Need some directions in how to simplify this hughe script

Fri Dec 14, 2018 12:39 am

Hello guys,

i'm completely new at scripting, but i mannaged to created a scipt that does the folllowing and i'm actualy proud of it

1st: check if an interface (ether2 ->... depending on how many eth interfaces) is running
2nd: if yes look if the neighbor is a mt
3th if Yes, set bridge to "bridgeVLAN" if no, set bridge to "bridgePUBLIC"

This for automated security reasons inside our networks
Mt -> 60 range IP's
public Users (PC, PS4,...) -> 20 range IP's

script:
do {
:if ([/interface get ether2 running] = true) do={
    :global CI2 0
    :global CI2 [/ip neighbor get [find interface="ether2"] platform]
    :if ($CI2="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether2"]}} 
    :if ($CI2="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether2"]}}}
:if ([/interface get ether3 running] = true) do={
    :global CI3 0
    :global CI3 [/ip neighbor get [find interface="ether3"] platform]
    :if ($CI3="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether3"]}} 
    :if ($CI3="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether3"]}}}   
:if ([/interface get ether4 running] = true) do={
    :global CI4 0
    :global CI4 [/ip neighbor get [find interface="ether4"] platform]
    :if ($CI4="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether4"]}} 
    :if ($CI4="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether4"]}}} 
:if ([/interface get ether5 running] = true) do={
    :global CI5 0
    :global CI5 [/ip neighbor get [find interface="ether5"] platform]
    :if ($CI5="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether5"]}} 
    :if ($CI5="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether5"]}}}
:if ([/interface get ether6 running] = true) do={
    :global CI6 0
    :global CI6 [/ip neighbor get [find interface="ether6"] platform]
    :if ($CI6="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether6"]}} 
    :if ($CI6="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether6"]}}} 
:if ([/interface get ether7 running] = true) do={
    :global CI7 0
    :global CI7 [/ip neighbor get [find interface="ether7"] platform]
    :if ($CI7="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether7"]}} 
    :if ($CI7="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether7"]}}} 
:if ([/interface get ether8 running] = true) do={
    :global CI8 0
    :global CI8 [/ip neighbor get [find interface="ether8"] platform]
    :if ($CI8="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether8"]}} 
    :if ($CI8="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether8"]}}}    
:if ([/interface get ether9 running] = true) do={
    :global CI9 0
    :global CI9 [/ip neighbor get [find interface="ether9"] platform]
    :if ($CI9="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether9"]}} 
    :if ($CI9="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether9"]}}} 
:if ([/interface get ether10 running] = true) do={
    :global CI10 0
    :global CI10 [/ip neighbor get [find interface="ether10"] platform]
    :if ($CI10="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether10"]}} 
    :if ($CI10="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether10"]}}} 
:if ([/interface get ether11 running] = true) do={
    :global CI11 0
    :global CI11 [/ip neighbor get [find interface="ether11"] platform]
    :if ($CI11="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether11"]}} 
    :if ($CI11="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether11"]}}}
:if ([/interface get ether12 running] = true) do={
    :global CI12 0
    :global CI12 [/ip neighbor get [find interface="ether12"] platform]
    :if ($CI12="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether12"]}} 
    :if ($CI12="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether12"]}}}  
:if ([/interface get ether13 running] = true) do={
    :global CI13 0
    :global CI13 [/ip neighbor get [find interface="ether13"] platform]
    :if ($CI13="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether13"]}} 
    :if ($CI13="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether13"]}}}
:if ([/interface get ether14 running] = true) do={
    :global CI14 0
    :global CI14 [/ip neighbor get [find interface="ether14"] platform]
    :if ($CI14="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether14"]}} 
    :if ($CI14="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether14"]}}}
:if ([/interface get ether15 running] = true) do={
    :global CI15 0
    :global CI15 [/ip neighbor get [find interface="ether15"] platform]
    :if ($CI15="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether15"]}} 
    :if ($CI15="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether15"]}}}
:if ([/interface get ether16 running] = true) do={
    :global CI16 0
    :global CI16 [/ip neighbor get [find interface="ether16"] platform]
    :if ($CI16="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether16"]}} 
    :if ($CI16="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether16"]}}}
:if ([/interface get ether17 running] = true) do={
    :global CI17 0
    :global CI17 [/ip neighbor get [find interface="ether17"] platform]
    :if ($CI17="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether17"]}} 
    :if ($CI17="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether17"]}}}
:if ([/interface get ether18 running] = true) do={
    :global CI18 0
    :global CI18 [/ip neighbor get [find interface="ether18"] platform]
    :if ($CI18="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether18"]}} 
    :if ($CI18="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether18"]}}}
:if ([/interface get ether19 running] = true) do={
    :global CI19 0
    :global CI19 [/ip neighbor get [find interface="ether12"] platform]
    :if ($CI19="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether19"]}} 
    :if ($CI19="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether19"]}}}
:if ([/interface get ether20 running] = true) do={
    :global CI20 0
    :global CI20 [/ip neighbor get [find interface="ether20"] platform]
    :if ($CI20="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether20"]}} 
    :if ($CI20="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether20"]}}} 
:if ([/interface get ether21 running] = true) do={
    :global CI21 0
    :global CI21 [/ip neighbor get [find interface="ether21"] platform]
    :if ($CI21="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether21"]}} 
    :if ($CI21="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether21"]}}}
:if ([/interface get ether22 running] = true) do={
    :global CI22 0
    :global CI22 [/ip neighbor get [find interface="ether22"] platform] 
    :if ($CI22="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether22"]}} 
    :if ($CI22="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether22"]}}}
:if ([/interface get ether23 running] = true) do={
    :global CI23 0
    :global CI23 [/ip neighbor get [find interface="ether23"] platform]
    :if ($CI23="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether23"]}} 
    :if ($CI23="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether23"]}}}
:if ([/interface get ether24 running] = true) do={
    :global CI24 0
    :global CI24 [/ip neighbor get [find interface="ether24"] platform] 
    :if ($CI24="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface="ether24"]}} 
    :if ($CI24="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface="ether24"]}}}
    }

The only thing is, if i run it via terminal command in things like hap lites , ac lites they start smoking...
So i need something that is much more simple, i know its possible, but i cant figure it out,
i've already tried tis:
    
:global CI 0  
    do {    
:foreach true in=[/interface find name~"ether[2-24]"running] do={
    :global CI [/ip neighbor get [find interface~"ether[2-24]"] platform]
    :if ($CI="MikroTik") do={interface bridge port {set bridge=bridgeVLAN [find interface~"ether[2-24]"]}}
    }}
but you can guess, it didnt work...

who can help me?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1302
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Need some directions in how to simplify this hughe script

Fri Dec 14, 2018 8:36 am

With the new 6.41+ RouterOS, you also need to set VLAN for each interface if you use more than default VLAN
So what I miss here is the
/interface bridge vlan settings

Also you could use an outer loop for your test. I am not capable of writing it but should be some like this
 :foreach i in=[/interface ethernet find] do={
    :if ([/interface get [/interface ethernet get $i name] running] = true) do={
       :global CI3 0
       :global CI3 [/ip neighbor get [find interface=[/interface ethernet get $i name]] platform]
       :if ($CI3="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface=[/interface ethernet get $i name]]}} 
       :if ($CI3="0") do={/interface bridge port {set bridge=bridgePUBLIC[find interface=[/interface ethernet get $i name]]}}} }
PS Not tested at all, just an idea.
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
User avatar
SteveG
just joined
Topic Author
Posts: 2
Joined: Thu Dec 13, 2018 11:51 pm
Location: Belgium
Contact:

Re: Need some directions in how to simplify this hughe script

Fri Dec 14, 2018 10:19 pm

Hey Jotne,

I tested the script and ofcourse, it didnt work out of the box
but it pointed me in the right direction,

after some mod's i have this:
:global A {"ether2";"ether3";"ether4";"ether5";"ether6";"ether7";"ether8";"ether9";"ether10";"ether11";"ether12";"ether13";"ether14";"ether15";"ether16";"ether17";"ether18";"ether19";"ether20";"ether21";"ether22";"ether23";"ether24"}
    
    do {
    :foreach i in=$A do={
    :if ([/interface get [/interface ethernet get $i name] running] = true) do={
       :global CI3 0
       :global CI3 [/ip neighbor get [find interface=[/interface ethernet get $i name]] platform]
       :if ($CI3="MikroTik") do={/interface bridge port {set bridge=bridgeVLAN [find interface=[/interface ethernet get $i name]]}} 
       :if ($CI3!="MikroTik") do={/interface bridge port {set bridge=bridgePUBLIC [find interface=[/interface ethernet get $i name]]}}} }
       }
and it looks like its working fine for me for now

thanks again
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1776
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: Need some directions in how to simplify this hughe script

Sun Dec 16, 2018 11:57 am

hey

you can create the name dynamically, ex: {:local i 10; :local port "ether$i"; :put $port;} => ether10

Who is online

Users browsing this forum: No registered users and 17 guests