Score:1

Checking if a mac-address is present on my network is working unreliably

in flag
Cas

I have an array of mac-addresses, and when they're all not present on the network, I want to do something. This was my original plan:

mac_addresses=('1' '2' '3')
arp_output=$(arp)
for level in "${mac_addresses[@]}"
do
   echo "$arp_output" | grep -iq "$level" || mac_count=$((mac_count+1))
done

if [[ "$mac_count" = "${#mac_addresses[@]}" ]]
then
   do something
fi

I was using the mac-address of my phone to test it. So only one element in the array: my phone's.

I noticed that the script was working unreliably. When my phone was connected to the network, the script did nothing. Good. When I disconnect my phone from the network (aka turn off wifi), it still does nothing, eventhough it should by this point (because my phone isn't present on the network anymore and that's the only in the array, so it should do something by this point).

So I tried a different way: using sudo nmap -sn 192.168.2.*. However, this also doesn't work.

When my phone is present, it doesn't do something. I disconnect my phone, and it does something. I re-connect my phone and it still does something, eventhough my phone is present on the network again. And it isn't about seconds. My phone has been on the network now again for 20 minutes and it still does something.

Is there a way to fix this? Is it because of some caching?

EDIT: [Response to Minsky]

See the following:

# "1" will be the mac-address of my phone
arp | grep -i "1" #phone connected to wifi
192.168.2.16             ether   1   C                     enp3s0

#PHONE IS NOW DISCONNECTED

arp | grep -i "1" #phone not connected to wifi
192.168.2.16             ether   1   C                     enp3s0
arp | grep -i "1" #phone not connected to wifi
192.168.2.16             ether   1   C                     enp3s0
arp | grep -i "1" #phone not connected to wifi
192.168.2.16             ether   1   C                     enp3s0

ping -b 192.168.2.255
Big output; 192.168.2.16 not visible in output

arp | grep -i "1" #phone not connected to wifi
192.168.2.16             ether   1   C                     enp3s0

#PHONE IS NOW RE-CONNECTED

arp | grep -i "1" #phone connected to wifi
192.168.2.16             ether   1   C                     enp3s0

sudo nmap -sn 192.168.2.* | grep "1"
#NO RESPONSE; EXIT CODE 1
ru flag
ARP only works within your network segment - is it outside of your local network range in a separate IP range? Does your wifi router / system have set up "privacy" which isolates devices from each other?
Cas avatar
in flag
Cas
no on both questions. The devices are both on `192.168.2.XXX` and the router isn't setup to isolate devices (isn't that just vlan's?).
Minsky avatar
mx flag
Does`arp` on pc follow your phone connection/disconnection? At least in my laptop, I have to run `ping -b <broadcastAddress>` first or it doesn't update the devices (sometimes). [[ removing prev. comments to keep it clean.]]
Cas avatar
in flag
Cas
@Minsky see edit on original post
meuh avatar
cn flag
The arp table is a cache, so old entries are only removed after a certain time. You should rely on a `ping` reply to see if a device is still there. See also `man ip neighbour` which replaces the `arp` command. You can flush the cache with `ip neigh flush dev eth0` where eth0 is the interface, before the broadcast ping.
Cas avatar
in flag
Cas
It's ofcourse way easier to ping the device. But I only have the mac-address, not the ip-address. So I can't know what ip-address to ping. I only have the mac-address. So I would first need to "convert" the mac-address to it's ip-address. However, how can you do that?
meuh avatar
cn flag
You are already doing a broadcast ping, `ping -b 192.168.2.255`. This should refill the arp table for any online devices with ip address 192.168.2.*.
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.