Score:0

Discover new IP address by calling MAC address

th flag

I have two devices that is located on a remote network:

  • A Raspberry Pi running Ubuntu 20.04. Let us call it Alex.
  • Raspberry Pi Pico Wireless running my code only. Let us call it Bob.

Alex acts as a gateway of sort for Bob only.

They both share the same local network, but for the exercise of the day you can assume they are both assigned dynamic ip from a local DHCP server and static ip is not an option.

I know the Alex can discover that its ip has changed by using dhcpcd-hooks.

But the tricky part is how to discover Bob has a new ip address and find out what the new ip address is?

It is safe to assume Bob cannot change its MAC address, so can Alex use that fact to discover the new ip address?

The failback solution is of course doing a nmap scanning of the entire subnet and find out which ip address responded with the right MAC address, but that sounds a bit excessive.

uz flag
Jos
Won't the `ip route` command faithfully display the correct gateway?
uz flag
Jos
If you know the MAC address you could also do `arp -a | grep [MAC address]`.
Lasse Michael Mølgaard avatar
th flag
`ip route` does not work because that means we have access to the router. `Alex` only acts as a gateway for `Bob` for one particular service only. `Bob` is basically a thermometer with built-in WiFi capabilities, so it has a very limited understanding on network setup. :-)
Lasse Michael Mølgaard avatar
th flag
My understanding of `arp` is that the table only shows MAC addresses from known ips, but if the IP has changed on both `Alex` and `Bob`, like for instance DHCP client table was purged, then they would not know how to find each other - unless I use `nmap` of the entire subnet.
uz flag
Jos
Couldn't you query the DHCP server?
Lasse Michael Mølgaard avatar
th flag
As far as I can tell there are no standard way of querying a server. The only way I have come up with besides `nmap` is by creating my own UDP based communication protocol and take advantage of the subnet broadcast address. I.e. sending a message to everyone on a specific port saying something like: "calling ab:cd:ef:12:34:56:78:90 - please answer!" ... and then make `Bob` listen for that UDP port.
uz flag
Jos
I have a `nmap -sn` running every couple of minutes to detect changes.
Lasse Michael Mølgaard avatar
th flag
That is a bit excessive. I would just use a TCP session with keepalive enabled to discover "something happened". If session dies and cannot be re-established, then fallback to UDP discovery using broadcast address. That way the client can use the source address in the UDP package as basis for the next TCP session which would make server learn the new ip address.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.