Community discussions

MikroTik App
 
sirlentschi
just joined
Topic Author
Posts: 5
Joined: Wed Jan 31, 2024 10:02 am

Assigning IP to device on port 4 depending on MAC of device connected to port 5

Wed Jan 31, 2024 10:36 am

Hello together,

I have checked the forum but did not find any case fitting my one.
I would like to assign an IP-address to a device PC1 on port "ether4" depending on the device connected to port "ether5" (PC2 or PC3). I wrote a script but it does not work (RouterOS 7.13.3):

:local macaddress5 [[:pick [/interface ethernet find where name="ether5"] 1 18]]
:local ipaddress

:log info ("MAC Address on ether5: " . $macaddress5)

:if ($macaddress5 = "MAC-OF_PC2") do={
:set ipaddress "10.150.2.1"
:log info ("Setting IP Address to: " . $ipaddress)
} else={
:if ($macaddress5 = "MAC-OF_PC3") do={
:set ipaddress "192.168.11.234"
:log info ("Setting IP Address to: " . $ipaddress)
} else={
:set ipaddress "192.168.88.130"
:log info ("Setting IP Address to: " . $ipaddress)
}
}

/ip dhcp-server lease set [find where mac-address="MAC-OF-PC1"] address=$ipaddress

The script is neither able to get the MAC-address of the device connected to port "ether5" ($macaddress5 is empty), nor assigns the (default) IP-address "192.168.88.130" to PC1 on "ether4".
PC1 = notebook, PC2 and PC3 = embedded hardware. Notebook and embedded HW communicate via SW simulator/webbrowser, which is why a certain IP-address of PC1 is needed....

Thanks!
 
sirlentschi
just joined
Topic Author
Posts: 5
Joined: Wed Jan 31, 2024 10:02 am

Re: Assigning IP to device on port 4 depending on MAC of device connected to port 5

Wed Jan 31, 2024 11:29 am

First of all, i would just like to know how to extract the mac-address of a device connected to port 5 by scripting. The command "/ip arp print as-value where address="192.168.88.253"" returns at least a list of data including the mac-address of the connected device. I need to detail this command in order to only extract the mac-address of the connected device (PC2 or PC3) and assign it to a variable.

A hint: the IP-addresses of devices PC2 and PC3 will always be static!
 
sirlentschi
just joined
Topic Author
Posts: 5
Joined: Wed Jan 31, 2024 10:02 am

Re: Assigning IP to device on port 4 depending on MAC of device connected to port 5

Thu Feb 01, 2024 4:29 pm

Got it! Searching for device PC2 via static IP address (arp entries!) and assigning an IP address to device PC1 accordingly:

/ip arp remove [ find where !complete ]

:local ipAddress "IP-ADDRESS-OF-PC2";
:local arpId [/ip arp find address=$ipAddress];
:local newIPAddress;

:log info ("Checking ARP entry for IP Address: " . $ipAddress);
:log info ("Result of /ip arp find ID: " . $arpId);

:if ($arpId != "") do={
:local macAddress [/ip arp get $arpId mac-address];
:log info ("ARP Entry found for IP Address: " . $ipAddress);
:log info ("MAC Address: " . $macAddress);

:if ($macAddress = "MAC-ADDRESS-OF-PC2") do={
:set newIPAddress "IP-ADDRESS-1";
} else={
:if ($macAddress = "MAC-ADDRESS-OF-PC3") do={
:set newIPAddress "IP-ADDRESS-2";
} else={
:set newIPAddress "IP-ADDRESS-3";
}
}
} else={
:log info ("ARP entry not found for IP Address: " . $ipAddress);
:set newIPAddress "IP-ADDRESS-3";
:log info ("ARP Table Contents:");

:local arpEntries [/ip arp find]

:foreach arpEntryId in=$arpEntries do={
:local ipAddressE [/ip arp get $arpEntryId address]
:log info " IP Address: $ipAddressE"
}
}
:log info ("Setting IP Address to: " . $newIPAddress);
/ip dhcp-server lease set [find where mac-address="MAC-ADDRESS-OF-PC1"] address=$newIPAddress;

}

Remove the "static" line ":local ipAddress "IP-PC2";" and create a list of different IP addresses instead to make the code more flexible.

BR
 
DeadStik
just joined
Posts: 20
Joined: Thu Jan 04, 2024 4:35 pm

Re: Assigning IP to device on port 4 depending on MAC of device connected to port 5

Fri Feb 02, 2024 6:12 pm

Could you not just assign the IPs to ether5 and use src-nat without having to script or even change the Notebook IP?
/ip address
add address=10.150.2.1/24 interface=ether5 network=10.150.2.0
add address=192.168.11.234/24 interface=ether5 network=192.168.11.0
/ip firewall nat
add action=src-nat chain=srcnat dst-address=10.150.2.? to-addresses=10.150.2.1
add action=src-nat chain=srcnat dst-address=192.168.11.? to-addresses=192.168.11.234