How to get IP, MAC, EtherPort for all currently active EtherPorts?
Ie. for ports of router/switch that have active/alive devices attached? I of course mean the IP and MAC of the attached device, and the EtherPort of the router/switch.
or/ip arp print
/ip arp print detail
You can try this script which will list all needed data into a file.How to get IP, MAC, EtherPort for all currently active EtherPorts?
Ie. for ports of router/switch that have active/alive devices attached? I of course mean the IP and MAC of the attached device, and the EtherPort of the router/switch.
{
:local ethlist {"ether1-WAN1";"ether2-WAN2";"ether3-LAN1";"ether4-LAN2"};
:local buffer;
:local fileName "address-list";
:foreach a in=$ethlist do={
:if ([/interface ethernet print as-value where name=$a && running=yes]) do={
:foreach i in=[/ip arp print as-value where interface=$a] do={
:set $buffer ($buffer." address=".$i->"address"." - mac-address=".$i->"mac-address"." - ETH=".$a."\n");
}
}
}
/file print file=$fileName where name="";
delay 1s;
/file set $fileName contents=$buffer;
}
Hi @Jotne, thanks. This is indeed good, but it says for all active interfaces just "interface=bridge".or/ip arp print/ip arp print detail
Hi @accarda, thanks for the script.You can try this script which will list all needed data into a file.How to get IP, MAC, EtherPort for all currently active EtherPorts?
Ie. for ports of router/switch that have active/alive devices attached? I of course mean the IP and MAC of the attached device, and the EtherPort of the router/switch.
You need to set your values for variable ethlist (this is an example), adding all of your ETH interfaces; the script will list only addresses from those that are running.Code: Select all{ :local ethlist {"ether1-WAN1";"ether2-WAN2";"ether3-LAN1";"ether4-LAN2"}; :local buffer; :local fileName "address-list"; :foreach a in=$ethlist do={ :if ([/interface ethernet print as-value where name=$a && running=yes]) do={ :foreach i in=[/ip arp print as-value where interface=$a] do={ :set $buffer ($buffer." address=".$i->"address"." - mac-address=".$i->"mac-address"." - ETH=".$a."\n"); } } } /file print file=$fileName where name=""; delay 1s; /file set $fileName contents=$buffer; }
Armando.
You can try in this way.
Hi @accarda, thanks for the script.
But I need this on CRS switch devices.These devices have 24+ or even 48+ interfaces (aka ports), so it is impractical to define all the interfaces manually. Isn't there a method to get all the interfaces as a list or as an array and put it programmatically into your "ethlist" above?
Other than that I just need the output to be printed, ie. putting the result into a file is not required. But this is no problem as I can replace that by a ":put ..."
{
:local ethlist;
:local buffer;
:local fileName "address-list";
:foreach i1 in [/interface ethernet find where running=yes] do={
:set $ethlist [/interface ethernet get $i1 name];
:foreach i2 in=$ethlist do={
:foreach i3 in=[/ip arp print as-value where interface=$i2] do={
:set $buffer ($buffer." address=".$i3->"address"." - mac-address=".$i3->"mac-address"." - ETH=".$i2."\n");
}
}
}
/file print file=$fileName where name="";
delay 1s;
/file set $fileName contents=$buffer;
}
[admin2@CRS326] /system/script> print
Flags: I - invalid
0 name="myTest1" owner="admin2" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon dont-require-permissions=no last-started=may/09/2020 10:54:53 run-count=6
...
1 name="myTest2" owner="admin2" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon dont-require-permissions=no run-count=0 source=
{
:local ethlist;
:local buffer;
:local fileName "address-list";
:foreach i1 in=[/interface ethernet find where running=yes] do={
:set $ethlist [/interface ethernet get $i1 name];
:foreach i2 in=$ethlist do={
:foreach i3 in=[/ip arp print as-value where interface=$i2] do={
:set $buffer ($buffer." address=".$i3->"address"." - mac-address=".$i3->"mac-address"." - ETH=".$i2."\n");
}
}
}
/file print file=$fileName where name="";
delay 1s;
/file set $fileName contents=$buffer;
}
[admin2@CRS326] /system/script> run myTest2
[admin2@CRS326] /system/script> /file/print
Columns: NAME, TYPE, SIZE, CREATION-TIME
# NAME TYPE SIZE CREATION-TIME
...
1 address-list.txt .txt file 0 may/16/2020 12:40:26
...
I don't know if it's about some wrong character during copy/past into this forum.@Armando, thx, but something isn't working here as the output is empty, s.b.
I added it to "/system script" under the name "myTest2". The editor in RouterOS said that there is a missing "=" in the first foreach line, I fixed it, and let it run, but the result is an empty output file:
Ok, no problem.It seems it's supposed to work also without "=":
[admin2@CRS326] /system/script> :foreach i1 in [/interface ethernet find where running=yes] do={ :put $i1; }
*1
*f
[admin2@CRS326] /system/script> :foreach i1 in=[/interface ethernet find where running=yes] do={ :put $i1; }
*1
*f
But, the error must be somewhere else as it does not print any final results.
Ok, I'll try to debug the script.
Thx for your help.
Thx, but there seems to be the following bug in this beta:just FYI, I have tried to add the = sign in the first couple of foreach and I have got the same syntax error even with ROS 6.46.6.
So I don't think it's about the beta that you are using.
Armando
[admin2@CRS326] /system/script> /ip arp print
Flags: D - DYNAMIC, P - PUBLISHED; C - COMPLETE
Columns: ADDRESS, MAC-ADDRESS, INTERFACE
# ADDRESS MAC-ADDRESS INTERF
0 DC 192.168.20.1 20:1A:06:5F:C0:3D bridge
1 DC 192.168.127.254 24:A4:3C:06:6C:2D bridge
2 DC 192.168.90.1 00:40:F4:A6:CE:E2 bridge
[admin2@CRS326] /system/script> /ip arp print as-value where interface="bridge"
[admin2@CRS326] /system/script> /ip arp print as-value where address="192.168.90.1"
[admin2@CRS326] /system/script> /ip arp print as-value where address=192.168.90.1
:put [/ip arp print as-value where address "192.168.90.1"]
.id=*1;address=192.168.20.1;comment=;interface=bridge;mac-address=20:1A:06:5F:C0:3D;.id=*2;address=192.168.127.254;comment=;interface=bridge;mac-address=24:A4:3C:06:6C:2D;.id=*3;address=192.168.90.1;comment=;interface=bridge;mac-address=00:40:F4:A6:CE:E2
:put [/ip arp print as-value where address="192.168.90.1"]
.id=*3;address=192.168.90.1;comment=;interface=bridge;mac-address=00:40:F4:A6:CE:E2
[user@rb0] > :put [/ip arp print as-value where address="192.168.0.1"]
.id=*10;address=192.168.0.1;comment=;interface=ether2-;mac-address=A8:7D:10:21:FB:2A
[user@rb0] >
It accepts it with or without the said "=", but here in this beta the result is an empty file.Actually I have just tried here from the terminal as well, adding the equal sign and I get only one record, as expected.
Do you get the same ?Code: Select all[user@rb0] > :put [/ip arp print as-value where address="192.168.0.1"] .id=*10;address=192.168.0.1;comment=;interface=ether2-;mac-address=A8:7D:10:21:FB:2A [user@rb0] >
This is done with 6.46.6. So that will work also in your beta.
At this point if you try the script that I have sent you (last one), I guess it should work to print out all fields, or do you still get syntax error ?
{
:local ethlist;
:local buffer;
:local fileName "address-list";
:foreach i1 in=[/interface find running=yes] do={:set $ethlist [/interface get $i1 value-name=name];
:foreach i2 in=$ethlist do={:foreach i3 in=[/ip arp print as-value where interface=$i2] do={:set $buffer ($buffer." address=".$i3->"address"." - mac-address=".$i3->"mac-address"." - interface=".$i2."\n")}}};
/file print file=$fileName where name="";
delay 1s;
/file set $fileName contents=$buffer;
}
/interface bridge host print
I don't use DHCP, but this still looks very interesting! I've an idea: with a little bit tricky programming this could serve as the basis for the solution... Will try it out. Thx!An alternate would be to use something like:Code: Select all/interface bridge host print
It won't get you the address, but it will get you mac-address and interface it is on. You could then combine this with data from the /ip dhcp-server lease of your router.
/interface bridge host print
Flags: D - DYNAMIC; L - LOCAL; E - EXTERNAL
Columns: MAC-ADDRESS, ON-INTERFACE, BRIDGE
# MAC-ADDRESS ON-INTE BRIDGE
0 D E 20:1A:06:5F:C0:3D ether15 bridge
1 D E 24:A4:3C:06:6C:2D ether1 bridge
2 DL C4:AD:34:78:E1:88 bridge bridge
3 DL C4:AD:34:78:E1:96 ether15 bridge
/system/script> :foreach i in=[/interface/bridge/host print detail as-value show-ids ] do={ :put $i; }
.id=*38;bridge=bridge;comment=;interface=ether15;mac-address=20:1A:06:5F:C0:3D;on-interface=ether15
.id=*36;bridge=bridge;comment=;interface=ether1;mac-address=24:A4:3C:06:6C:2D;on-interface=ether1
.id=*39;bridge=bridge;comment=;interface=bridge;mac-address=C4:AD:34:78:E1:88;on-interface=bridge
.id=*37;bridge=bridge;comment=;interface=ether15;mac-address=C4:AD:34:78:E1:96;on-interface=ether15
Code: Select all# Algorithm (pseudo code): foreach i in /interface/bridge/host f1 = is $i->MAC in /ip/arp ? f2 = is $i->MAC not in /interface/ethernet ? fOk = f1 AND f2 if fOK print IP, MAC, interface
{
:local ip
/interface bridge host
:foreach ID in=[find] do={
:local inf [get $ID interface]
:local mac [get $ID mac-address]
:local idmac [/ip arp find mac-address="$mac"]
:if ([:len $idmac] > 0) do={
:set ip [/ip arp get $idmac address]
}
:put "interface=$inf mac=$mac ip=$ip"
}
}
{
:local ip
/interface bridge host
:foreach ID in=[find] do={
:local inf [get $ID interface]
:local mac [get $ID mac-address]
:local idmac [/ip arp find mac-address="$mac"]
:if ([:len $idmac] > 0) do={
:set ip [/ip arp get $idmac address]
:put "interface=$inf mac=$mac ip=$ip"
}
}
}
Actually in my case I don't use the bridge (each port is an independed interface), so what I tested was based on that.Armando, my debug analysis indicates that "/ip arp" does not have a unique (ie. common in both) key/value pair for lookup in "/interface ethernet",
so then it seems it can't be done with "/interface ethernet".
There must be some other location like bridge or switch ...
Update:
see also this related new feature request I just made: viewtopic.php?f=1&t=161253
Code: Select all[admin2@CRS326] /system/script> print where name="myTest3" Flags: I - invalid 2 name="myTest3" owner="admin2" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon dont-require-permissions=no run-count=0 source= { :local ip /interface bridge host :foreach ID in=[find] do={ :local inf [get $ID interface] :local mac [get $ID mac-address] :local idmac [/ip arp find mac-address="$mac"] :if ([:len $idmac] > 0) do={ :set ip [/ip arp get $idmac address] :put "interface=$inf mac=$mac ip=$ip" } } } [admin2@CRS326] /system/script> run myTest3 interface=ether15 mac=20:1A:06:5F:C0:3D ip=192.168.20.1 interface=ether1 mac=24:A4:3C:06:6C:2D ip=192.168.127.254
Thank you all; it was IMO a very fruitful discussion; I as a newbie to MT/ROS-scripting learned much.Actually in my case I don't use the bridge (each port is an independed interface), so what I tested was based on that.Armando, my debug analysis indicates that "/ip arp" does not have a unique (ie. common in both) key/value pair for lookup in "/interface ethernet",
so then it seems it can't be done with "/interface ethernet".
There must be some other location like bridge or switch ...
Update:
see also this related new feature request I just made: viewtopic.php?f=1&t=161253
Now I see all these differences that you have seen so far, beside the initial errors.
/interface ethernet switch host print
I think you can get that from "/interface/ethernet/print"I did try to see if it was possible to get the mac from all interface, not just bridge, but still no luck.
But if there are some I do miss and its possible, it should be possible to make a script that covers both bridges and ports.
give only information about the interface it self and its mac address. Not mac-address that ar connected to it./interface ethernet print
[xxx] > /interface ethernet print
Flags: X - disabled, R - running, S - slave
# NAME MTU MAC-ADDRESS ARP
0 RS ether1 1500 00:50:5A:AA:5F:75 enabled
[xxxx] > /interface ethernet print detail
Flags: X - disabled, R - running, S - slave
0 RS name="ether1" default-name="ether1" mtu=1500 mac-address=00:50:5A:AA:5F:75 orig-mac-address=00:50:5A:AA:5F:75 arp=enabled arp-timeout=auto
loop-protect=default loop-protect-status=off loop-protect-send-interval=5s loop-protect-disable-time=5m disable-running-check=yes auto-negotiation=yes
advertise=10M-half,10M-full,100M-half,100M-full,1000M-full full-duplex=yes tx-flow-control=off rx-flow-control=off cable-settings=default speed=1Gbps
Ah, ok, I see: you are looking for a location similar to "/interface bridge host" that gives the currently attached devices, for ROS environments where no bridge is defined.This:give only information about the interface it self and its mac address. Not mac-address that ar connected to it./interface ethernet print
Code: Select all[xxx] > /interface ethernet print Flags: X - disabled, R - running, S - slave # NAME MTU MAC-ADDRESS ARP 0 RS ether1 1500 00:50:5A:AA:5F:75 enabled [xxxx] > /interface ethernet print detail Flags: X - disabled, R - running, S - slave 0 RS name="ether1" default-name="ether1" mtu=1500 mac-address=00:50:5A:AA:5F:75 orig-mac-address=00:50:5A:AA:5F:75 arp=enabled arp-timeout=auto loop-protect=default loop-protect-status=off loop-protect-send-interval=5s loop-protect-disable-time=5m disable-running-check=yes auto-negotiation=yes advertise=10M-half,10M-full,100M-half,100M-full,1000M-full full-duplex=yes tx-flow-control=off rx-flow-control=off cable-settings=default speed=1Gbps
Code: Select all/interface ethernet switch host print Flags: D - DYNAMIC, I - INVALID Columns: SWITCH, MAC-ADDRESS, PORTS, TIMEOUT, DROP, MIRROR, VLAN-ID # SWITCH MAC-ADDRESS PORTS TI DR MI V 0 D switch1 20:1A:06:5F:C0:3D ether15 0s no no 1 1 D switch1 C4:AD:34:78:E1:88 switch1-cpu 0s no no 1 2 D switch1 24:A4:3C:06:6C:2D ether1 0s no no 1