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
0 Answers