Skip to content
Apr 29 / Greg

Mikrotik Script To Add CPE Names To Switch Ports

In one of my new build MDUs, the cabling got all scrambled so my in room Mikrotik routers got all scrambled up. I needed a way to go through each switch and update the interface comment to match what room it is associated with.

Each CPE is named Room101, Room225, etc. If I connect to the switch and issue a “/ip neighbor print detail” I get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[admin@IDF1-Sw1] > ip neighbor print de
 0 interface=ether1,bridge1 mac-address=B8:69:F4:CC:63:E1 identity="Room216" platform="MikroTik" version="6.46.5 (stable)" unpack=none 
   age=47s interface-name="bridge1/ether1" system-description="MikroTik RouterOS 6.46.5 (stable) RBD52G-5HacD2HnD" 
   system-caps=bridge,wlan-ap,router system-caps-enabled=bridge,wlan-ap,router 
 
 1 interface=ether1,bridge1 mac-address=B8:69:F4:CC:63:E6 identity="Room216" platform="MikroTik" version="6.46.5 (stable)" unpack=none 
   age=47s uptime=2d2h56m23s software-id="W3J3-TN8Y" board="RBD52G-5HacD2HnD" ipv6=no interface-name="bridge1/ether1" system-caps="" 
   system-caps-enabled="" 
 
 2 interface=ether2,bridge1 mac-address=B8:69:F4:CC:64:0B identity="Room260" platform="MikroTik" version="6.46.5 (stable)" unpack=none 
   age=41s interface-name="bridge1/ether1" system-description="MikroTik RouterOS 6.46.5 (stable) RBD52G-5HacD2HnD" 
   system-caps=bridge,wlan-ap,router system-caps-enabled=bridge,wlan-ap,router 
 
 3 interface=ether2,bridge1 mac-address=B8:69:F4:CC:64:10 identity="Room260" platform="MikroTik" version="6.46.5 (stable)" unpack=none 
   age=41s uptime=2d2h55m26s software-id="7FC3-R521" board="RBD52G-5HacD2HnD" ipv6=no interface-name="bridge1/ether1" system-caps="" 
   system-caps-enabled=""

I’m connecting these hAPac^2s to a CRS328 switch(an awesome combo!). I need to write a script that will list the neighbor table, filter on just ether interfaces and also filter to make sure the identity of the remote device starts with “Room”.

The script itself.

1
2
3
4
:local cpes [/ip neighbor print detail as-value where interface~"ether" and identity~"Room"];
:foreach cpe in=$cpes do={
    /interface set [/interface find name=[:pick ($cpe->"interface") 0 [:find ($cpe->"interface") "b"]]] comment=($cpe->"identity");
}

The script in terminal copy/paste form:

1
2
3
4
5
6
7
8
/system script
add dont-require-permissions=no name=interface-comments owner=admin policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":local cpes [/ip neighbor print detail as-value where interf\
    ace~\"ether\" and identity~\"Room\"];\r\
    \n:foreach cpe in=\$cpes do={\r\
    \n    /interface set [/interface find name=[:pick (\$cpe->\"interface\") 0 [:find (\$cpe->\"interface\") \"b\"]]] comment=(\$cpe->\"i\
    dentity\");\r\
    \n}"

Here’s an example of it alllllll compiled into one line for use with Ansible:

1
:local cpes [/ip neighbor print detail as-value where interface~"ether" and identity~"Room"]; :foreach cpe in=$cpes do={/interface set [/interface find name=[:pick ($cpe->"interface") 0 [:find ($cpe->"interface") "b"]]] comment=($cpe->"identity");}

Speaking of ansible you can find the playbook here.

Completion of the script looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[admin@IDF1-Sw1] /interface> print
Flags: D - dynamic, X - disabled, R - running, S - slave 
 #     NAME                                TYPE       ACTUAL-MTU L2MTU  MAX-L2MTU MAC-ADDRESS      
 0  RS ;;; Room216
       ether1                              ether            1500  1592      10218 CC:2D:E0:63:4F:20
 1  RS ;;; Room260
       ether2                              ether            1500  1592      10218 CC:2D:E0:63:4F:21
 2  RS ;;; Room270
       ether3                              ether            1500  1592      10218 CC:2D:E0:63:4F:22
 3  RS ;;; Room258
       ether4                              ether            1500  1592      10218 CC:2D:E0:63:4F:23
 4  RS ;;; Room272
       ether5                              ether            1500  1592      10218 CC:2D:E0:63:4F:24
 5  RS ;;; Room266
       ether6                              ether            1500  1592      10218 CC:2D:E0:63:4F:25
 6  RS ;;; Room256
       ether7                              ether            1500  1592      10218 CC:2D:E0:63:4F:26
 7  RS ;;; Room264
       ether8                              ether            1500  1592      10218 CC:2D:E0:63:4F:27

In the above scripts you can edit the interface~”ether” and identity~”Room” to be more tailored to your needs.

Good luck and automate all the things with Ansible!

Leave a Comment

 

*