Login Register





Notification on device joining network filter_list
Author
Message
Notification on device joining network #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:
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 network
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.
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

Reply

RE: Notification on device joining network #2
Quote:I did some testing with the phone and iPad and found out they only appear and disappear while I'm not interacting with them
You should Implement a Known devices setting, thereby every device that connects (and you allow) at any given time, will be permanently displayed- Irrespective whether they're actively connected to your network.

Of course, when they're connected, It will show as per your "joined the network" feature.

Reply

RE: Notification on device joining network #3
(07-28-2020, 06:20 AM)mothered Wrote:
Quote:I did some testing with the phone and iPad and found out they only appear and disappear while I'm not interacting with them
You should Implement a Known devices setting, thereby every device that connects (and you allow) at any given time, will be permanently displayed- Irrespective whether they're actively connected to your network.

Of course, when they're connected, It will show as per your "joined the network" feature.

That wouldn't be an ideal fix as I want to use that script for stuff like lets say turn off the lights in my appartment etc once my phone leaves the network. I guess I will just go for a mix of your idea and the check last x scans one I had to flag the devices that tend to have this behaviour.

Reply






Users browsing this thread: 1 Guest(s)