{"id":6258,"date":"2020-04-29T17:44:41","date_gmt":"2020-04-29T23:44:41","guid":{"rendered":"http:\/\/gregsowell.com\/?p=6258"},"modified":"2020-04-29T17:46:18","modified_gmt":"2020-04-29T23:46:18","slug":"mikrotik-script-to-add-cpe-names-to-switch-ports","status":"publish","type":"post","link":"https:\/\/gregsowell.com\/?p=6258","title":{"rendered":"Mikrotik Script To Add CPE Names To Switch Ports"},"content":{"rendered":"<p>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.<\/p>\n<p>Each CPE is named Room101, Room225, etc.  If I connect to the switch and issue a &#8220;\/ip neighbor print detail&#8221; I get:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">[admin@IDF1-Sw1] &gt; ip neighbor print de\r\n 0 interface=ether1,bridge1 mac-address=B8:69:F4:CC:63:E1 identity=&quot;Room216&quot; platform=&quot;MikroTik&quot; version=&quot;6.46.5 (stable)&quot; unpack=none \r\n   age=47s interface-name=&quot;bridge1\/ether1&quot; system-description=&quot;MikroTik RouterOS 6.46.5 (stable) RBD52G-5HacD2HnD&quot; \r\n   system-caps=bridge,wlan-ap,router system-caps-enabled=bridge,wlan-ap,router \r\n\r\n 1 interface=ether1,bridge1 mac-address=B8:69:F4:CC:63:E6 identity=&quot;Room216&quot; platform=&quot;MikroTik&quot; version=&quot;6.46.5 (stable)&quot; unpack=none \r\n   age=47s uptime=2d2h56m23s software-id=&quot;W3J3-TN8Y&quot; board=&quot;RBD52G-5HacD2HnD&quot; ipv6=no interface-name=&quot;bridge1\/ether1&quot; system-caps=&quot;&quot; \r\n   system-caps-enabled=&quot;&quot; \r\n\r\n 2 interface=ether2,bridge1 mac-address=B8:69:F4:CC:64:0B identity=&quot;Room260&quot; platform=&quot;MikroTik&quot; version=&quot;6.46.5 (stable)&quot; unpack=none \r\n   age=41s interface-name=&quot;bridge1\/ether1&quot; system-description=&quot;MikroTik RouterOS 6.46.5 (stable) RBD52G-5HacD2HnD&quot; \r\n   system-caps=bridge,wlan-ap,router system-caps-enabled=bridge,wlan-ap,router \r\n\r\n 3 interface=ether2,bridge1 mac-address=B8:69:F4:CC:64:10 identity=&quot;Room260&quot; platform=&quot;MikroTik&quot; version=&quot;6.46.5 (stable)&quot; unpack=none \r\n   age=41s uptime=2d2h55m26s software-id=&quot;7FC3-R521&quot; board=&quot;RBD52G-5HacD2HnD&quot; ipv6=no interface-name=&quot;bridge1\/ether1&quot; system-caps=&quot;&quot; \r\n   system-caps-enabled=&quot;&quot;<\/code><\/pre>\n<p>I&#8217;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 &#8220;Room&#8221;.<\/p>\n<p>The script itself.<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">:local cpes [\/ip neighbor print detail as-value where interface~&quot;ether&quot; and identity~&quot;Room&quot;];\r\n:foreach cpe in=$cpes do={\r\n    \/interface set [\/interface find name=[:pick ($cpe-&gt;&quot;interface&quot;) 0 [:find ($cpe-&gt;&quot;interface&quot;) &quot;b&quot;]]] comment=($cpe-&gt;&quot;identity&quot;);\r\n}<\/code><\/pre>\n<p>The script in terminal copy\/paste form:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">\/system script\r\nadd dont-require-permissions=no name=interface-comments owner=admin policy=\\\r\n    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=&quot;:local cpes [\/ip neighbor print detail as-value where interf\\\r\n    ace~\\&quot;ether\\&quot; and identity~\\&quot;Room\\&quot;];\\r\\\r\n    \\n:foreach cpe in=\\$cpes do={\\r\\\r\n    \\n    \/interface set [\/interface find name=[:pick (\\$cpe-&gt;\\&quot;interface\\&quot;) 0 [:find (\\$cpe-&gt;\\&quot;interface\\&quot;) \\&quot;b\\&quot;]]] comment=(\\$cpe-&gt;\\&quot;i\\\r\n    dentity\\&quot;);\\r\\\r\n    \\n}&quot;<\/code><\/pre>\n<p>Here&#8217;s an example of it alllllll compiled into one line for use with Ansible:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">:local cpes [\/ip neighbor print detail as-value where interface~&quot;ether&quot; and identity~&quot;Room&quot;]; :foreach cpe in=$cpes do={\/interface set [\/interface find name=[:pick ($cpe-&gt;&quot;interface&quot;) 0 [:find ($cpe-&gt;&quot;interface&quot;) &quot;b&quot;]]] comment=($cpe-&gt;&quot;identity&quot;);}<\/code><\/pre>\n<p>Speaking of ansible you can <a href=\"https:\/\/github.com\/gregsowell\/ansible-mikrotik\/blob\/master\/mtk-switch-interface-desc.yml\">find the playbook here<\/a>.<\/p>\n<p>Completion of the script looks like this:<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">[admin@IDF1-Sw1] \/interface&gt; print\r\nFlags: D - dynamic, X - disabled, R - running, S - slave \r\n #     NAME                                TYPE       ACTUAL-MTU L2MTU  MAX-L2MTU MAC-ADDRESS      \r\n 0  RS ;;; Room216\r\n       ether1                              ether            1500  1592      10218 CC:2D:E0:63:4F:20\r\n 1  RS ;;; Room260\r\n       ether2                              ether            1500  1592      10218 CC:2D:E0:63:4F:21\r\n 2  RS ;;; Room270\r\n       ether3                              ether            1500  1592      10218 CC:2D:E0:63:4F:22\r\n 3  RS ;;; Room258\r\n       ether4                              ether            1500  1592      10218 CC:2D:E0:63:4F:23\r\n 4  RS ;;; Room272\r\n       ether5                              ether            1500  1592      10218 CC:2D:E0:63:4F:24\r\n 5  RS ;;; Room266\r\n       ether6                              ether            1500  1592      10218 CC:2D:E0:63:4F:25\r\n 6  RS ;;; Room256\r\n       ether7                              ether            1500  1592      10218 CC:2D:E0:63:4F:26\r\n 7  RS ;;; Room264\r\n       ether8                              ether            1500  1592      10218 CC:2D:E0:63:4F:27<\/code><\/pre>\n<p>In the above scripts you can edit the interface~&#8221;ether&#8221; and identity~&#8221;Room&#8221; to be more tailored to your needs.<\/p>\n<p>Good luck and automate all the things with Ansible!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of my new build MDUs, the cabling got all scrambled so my in room Mikrotik routers got all scrambled up. I needed\u2026<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49,6,8],"tags":[],"class_list":["post-6258","post","type-post","status-publish","format-standard","hentry","category-ansible","category-mikrotik","category-networking"],"_links":{"self":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/6258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6258"}],"version-history":[{"count":4,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/6258\/revisions"}],"predecessor-version":[{"id":6262,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/6258\/revisions\/6262"}],"wp:attachment":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}