Notification on device joining network 07-27-2020, 11:27 PM
#1
Hey,
I'm currently working on a script that detects if a device joins or leaves my network.
The idea behind it is that I want to run specific tasks once I come home and once I leave. Another interesting usecase would be to detect if other people you live with are at home or not. I first thought about using the phones internal ipv4 addresses as indentification but that's not very secure as routers tend to give out ips dynamically depending on their configuration. So my second thought was to scan for all mac addresses on the network and simply check if they match my known list. I started using arp to get a list of devices and noticed my router was configured in a way where lan devices could only see lan devices while wifi devices could only see wifi devices for some reason. I somehow managed to fix it and decided to use a simple nmap portscan and it worked but I noticed some weird behaviour. I tried to continously log which devices appear and disappear from my arp list and that are the results:
I looked up the ips and found out these 3 devices are my iPad my phone and my FireTV Stick. I did some testing with the phone and iPad and found out they only appear and disappear while I'm not interacting with them. Is that a thing modern devices do to save some battery or something similar or is that an arp related problem? My PC and Laptop are visible on the arp table the whole time and just disappear when I disconnect them from the network. Does anybody have an idea how to fix this? My current idea is to compare the last x scans to detect which devices are continously reappearing.
I gotta admit that my knowledge about networking is quite limited and I usually don't work with python so sorry for the bad code and my retarded assumptions.
I'm currently working on a script that detects if a device joins or leaves my network.
The idea behind it is that I want to run specific tasks once I come home and once I leave. Another interesting usecase would be to detect if other people you live with are at home or not. I first thought about using the phones internal ipv4 addresses as indentification but that's not very secure as routers tend to give out ips dynamically depending on their configuration. So my second thought was to scan for all mac addresses on the network and simply check if they match my known list. I started using arp to get a list of devices and noticed my router was configured in a way where lan devices could only see lan devices while wifi devices could only see wifi devices for some reason. I somehow managed to fix it and decided to use a simple nmap portscan and it worked but I noticed some weird behaviour. I tried to continously log which devices appear and disappear from my arp list and that are the results:
Code:
[23:48:58] 192.168.178.45 left the network
[23:49:02] 192.168.178.51 joined the network
[23:49:05] 192.168.178.44 left the network
[23:49:05] 192.168.178.51 left the network
[23:49:08] 192.168.178.44 joined the network
[23:49:08] 192.168.178.45 joined the network
[23:49:24] 192.168.178.56 joined the network
[23:49:31] 192.168.178.56 left the network
[23:49:41] 192.168.178.45 left the network
[23:49:47] 192.168.178.51 joined the network
[23:49:51] 192.168.178.45 joined the network
[23:49:51] 192.168.178.51 left the networkI gotta admit that my knowledge about networking is quite limited and I usually don't work with python so sorry for the bad code and my retarded assumptions.
Code:
from nmap import PortScanner
from datetime import datetime
network = '192.168.178.1/24'
portScanner: PortScanner = PortScanner()
try:
  portScanner.scan(hosts=network, arguments='-sn')
except:
  print("An exception occurred while scanning the network!")
previousNetworkDevices = []
for x in portScanner.all_hosts():
  if len(portScanner[x]['addresses']) > 1:
    previousNetworkDevices.append((x, portScanner[x]['addresses']['mac']))
  else:
    previousNetworkDevices.append((x, 'unknown'))
while True:
  try:
    portScanner.scan(hosts=network, arguments='-sn')
  except:
    print("An exception occurred while scanning the network!")
  currentNetworkDevices = []
  for x in portScanner.all_hosts():
    if len(portScanner[x]['addresses']) > 1:
      currentNetworkDevices.append((x, portScanner[x]['addresses']['mac']))
    else:
      currentNetworkDevices.append((x, 'unknown'))
  for networkDevice in currentNetworkDevices:
    if networkDevice not in previousNetworkDevices:
      print('[{}] {} joined the network'.format(datetime.now().strftime("%H:%M:%S"), networkDevice[0]))
  for networkDevice in previousNetworkDevices:
    if networkDevice not in currentNetworkDevices:
      print('[{}] {} left the network'.format(datetime.now().strftime("%H:%M:%S"), networkDevice[0]))
  previousNetworkDevices = currentNetworkDevices


![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)











