Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

find mobiles at local network/ router

Wed May 06, 2015 8:46 am

Is there a way or code in Python to detect if mobile phones are connected to the router/ local network?

I like to use that for switching off heating by my smart thermostat?
Because if a phone is connected somebody is at home

help appriciated

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: find mobiles at local network/ router

Thu May 07, 2015 9:04 am

Nobody?

User avatar
B.Goode
Posts: 10936
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: find mobiles at local network/ router

Thu May 07, 2015 9:43 am

Canedje wrote:Is there a way or code in Python to detect if mobile phones are connected to the router/ local network?
Thinking aloud. (Or should that be 'thinking allowed?')

Most things you can do from the command line can also be scripted in python.

There are command line tools such as nmap that can scan for active devices on your local network.

Only you with your local knowledge can know whether a particular IP address on your network is being used by a device that is also capable of being used as a cellphone.

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: find mobiles at local network/ router

Thu May 07, 2015 9:46 am

B.Goode wrote:
Canedje wrote:Is there a way or code in Python to detect if mobile phones are connected to the router/ local network?
Thinking aloud. (Or should that be 'thinking allowed?')

Most things you can do from the command line can also be scripted in python.

There are command line tools such as nmap that can scan for active devices on your local network.

Only you with your local knowledge can know whether a particular IP address on your network is being used by a device that is also capable of being used as a cellphone.

Thanks for the help.
This is a starting idea.

I think the IP address is not enough because it can be variable. I do think I need the do something with MAC addresses.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: find mobiles at local network/ router

Thu May 07, 2015 10:06 am

Canedje wrote:I do think I need the do something with MAC addresses.
Yes, usually. Is this 'your' setup, or someone else's?

Most routers can be setup to give static addresses... and to do that you need to know the mac address of the device that will be attaching and then you assign the static address to match that mac address in the router.

If you have a dhcp server you can also set the static address for the given mac address there.
marcus
:ugeek:

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: find mobiles at local network/ router

Thu May 07, 2015 10:37 am

MarkHaysHarris777 wrote:
Canedje wrote:I do think I need the do something with MAC addresses.
Yes, usually. Is this 'your' setup, or someone else's?

Most routers can be setup to give static addresses... and to do that you need to know the mac address of the device that will be attaching and then you assign the static address to match that mac address in the router.

If you have a dhcp server you can also set the static address for the given mac address there.
Thanks.

But how do I read the available MAC/IP addresses from the router/network?
Can somebody give me some example code?
Even in script or Python?

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: find mobiles at local network/ router

Thu May 07, 2015 10:43 am

Canedje wrote:But how do I read the available MAC/IP addresses from the router/network?
ping them

User avatar
rpdom
Posts: 18002
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: find mobiles at local network/ router

Thu May 07, 2015 10:53 am

Scanning your subnet with nmap should populate your ARP table with MAC to IP mappings. You can then look through the ARP table to check for devices you are interested in by using /usr/sbin/arp

bullwinkle
Posts: 117
Joined: Wed Jan 09, 2013 12:14 pm

Re: find mobiles at local network/ router

Thu May 07, 2015 12:21 pm

Hi

I did something like this where I scanned for unknown devices on my network. You could modify my stuff to meet your needs
It uses nmap and bash script. Files found here....
http://dumb-looks-free.blogspot.fr/2014 ... vices.html

good luck

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: find mobiles at local network/ router

Thu May 07, 2015 1:03 pm

bullwinkle wrote:Hi

I did something like this where I scanned for unknown devices on my network. You could modify my stuff to meet your needs
It uses nmap and bash script. Files found here....
http://dumb-looks-free.blogspot.fr/2014 ... vices.html

good luck
Thanks this looks like a very good start and help

One question about this line:
nmap 192.168.1.0/24

This is your router IP address range?
My range is going to 250 I do need to change the line by:
nmap 192.168.1.0/250?

scotty101
Posts: 4162
Joined: Fri Jun 08, 2012 6:03 pm

Re: find mobiles at local network/ router

Thu May 07, 2015 1:17 pm

Canedje wrote: One question about this line:
nmap 192.168.1.0/24

This is your router IP address range?
My range is going to 250 I do need to change the line by:
nmap 192.168.1.0/250?
Not quite. Read the wikipedia entry on subnets for more info
http://en.wikipedia.org/wiki/Subnetwork
For example, 192.168.1.0/24 is the prefix of the Internet Protocol Version 4 network starting at the given address, having 24 bits allocated for the network prefix, and the remaining 8 bits reserved for host addressing.
Roughly translated. The last 8 bits are available for addressing each device connected to your router so the range 192.168.1.0-192.168.1.254 will be considered (except 255 which is the broadcast address)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
DougieLawson
Posts: 40561
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: find mobiles at local network/ router

Thu May 07, 2015 1:21 pm

sudo apt-get install ipcalc
ipcalc -n -b `hostname -I`

That gets you the details of your LAN based on your current IP address.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
bobstro
Posts: 193
Joined: Wed Feb 05, 2014 6:48 am
Location: Central Massachusetts, US
Contact: Website

Re: find mobiles at local network/ router

Thu May 07, 2015 1:36 pm

These are techniques to determine if a device is active on your network, but I'm understanding that you want to control your heating only when people are active and present. The moment you ping your router, whether manually or by program, it will think something's "alive".

If your RPi is your DHCP server, you can tell when a device gets a new IP address lease, which is at least an indication that the device is active. dnsmasq can easily be configured to assign specific address ranges based on device type, for example, so you'd easily be able to discern phones from desktops without having to know every possible connecting MAC address up front. For example, here's a redacted dump of my /var/lib/misc/dnsmasq.leases file after my sons visited:

Code: Select all

1431137959 60:be:b5:XX:XX:XX 172.16.16.117 android-XXXXX1 01:60:be:b5:XX:XX:XX
1431123335 dc:9b:9c:XX:XX:XX 172.16.16.109 XXiPhone 01:dc:9b:9c:XX:XX:XX
1431258352 64:66:b3:XX:XX:XX 172.16.16.124 raspi06 *
1431165215 64:66:b3:XX:XX:XX 172.16.16.86 raspi07 *
1431148159 cc:6d:a0:XX:XX:XX 172.16.16.71 NP-13C1CFXXXXXX *
1431260545 fc:c2:de:XX:XX:XX 172.16.16.121 android-XXXXX2 01:fc:c2:de:XX:XX:XX
1431247578 d8:90:e8:XX:XX:XX 172.16.16.111 android-XXXXX3 01:d8:90:e8:XX:XX:XX
1431236319 00:1a:98:XX:XX:XX 172.16.16.84 android-XXXXX4 *
This gives me 3 relevant bits of data for each active device:

1. The current IP address assigned in the DHCP lease. In my case, all client devices use DHCP, so those are the only ones I'd be interested in for your scenario.
2. The name the device used when querying the DHCP server. All of my Android devices use android-XXXXXX, and my RPis use their hostname (raspiXX), while Rokus use NP-XXXXXXXXXXXX so telling them apart is easy.
3. The device MAC address. This is useful because the 1st 3 bytes of a MAC address designate a manufacturer (although this can be changed, it's not common to do so). My son's iPhone uses a name that I couldn't easily predict, but I can tell that it's an Apple device based on the 1st 3 bytes of the MAC address. I can also pick out my Motorola smartphone:

DC-9B-9C-XX-XX-XX Apple, USA
60-BE-B5-XX-XX-XX Motorola Mobility LLC, USA
FC-C2-DE-XX-XX-XX Murata Manufacturing Co., Ltd., JAPAN
64-66-B3-XX-XX-XX TP-LINK TECHNOLOGIES CO., LTD., CHINA
D8-90-E8-XX-XX-XX Samsung Electronics Co.,Ltd, SOUTH KOREA
00-1A-98-XX-XX-XX Asotel Communication Limited Taiwan Branch, TAIWAN
CC-6D-A0-XX-XX-XX Roku, Inc., USA
00-0D-4B-XX-XX-XX Roku, LLC, USA

dnsmasq can be configured to assign different IP parameters based on any of these patterns. The beauty of doing it on the DHCP server (e.g. RPi) is that I don't have to do any network probes which themselves might look like "human" traffic, and I have access to a lot of device info without having to know about them up front (e.g. building a list of MAC addresses). If I set a short DHCP lease time, for example, for anything requesting a lease with the name android-XXXXXX, the leases disappear when the device is no longer present.

That still just means the phone is in your home though, and certainly not in a specific room or necessarily near a user. My IP subnet can be pinged well out into my yard and on the street. You also have no means of detecting user with a device that's not on your wifi or local network, or who has no device on them.

How localized do you want detection to be? House or room level? Might passive IR sensors be more effective at detecting bodies versus devices? Do you want to know when anybody is present, or only care about certain tech-savvy individuals? What about pets? What if the device goes into standby and is not active on the network for hours?

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: find mobiles at local network/ router

Fri May 08, 2015 6:59 am

bobstro wrote:These are techniques to determine if a device is active on your network, but I'm understanding that you want to control your heating only when people are active and present. The moment you ping your router, whether manually or by program, it will think something's "alive".

If your RPi is your DHCP server, you can tell when a device gets a new IP address lease, which is at least an indication that the device is active. dnsmasq can easily be configured to assign specific address ranges based on device type, for example, so you'd easily be able to discern phones from desktops without having to know every possible connecting MAC address up front. For example, here's a redacted dump of my /var/lib/misc/dnsmasq.leases file after my sons visited:

Code: Select all

1431137959 60:be:b5:XX:XX:XX 172.16.16.117 android-XXXXX1 01:60:be:b5:XX:XX:XX
1431123335 dc:9b:9c:XX:XX:XX 172.16.16.109 XXiPhone 01:dc:9b:9c:XX:XX:XX
1431258352 64:66:b3:XX:XX:XX 172.16.16.124 raspi06 *
1431165215 64:66:b3:XX:XX:XX 172.16.16.86 raspi07 *
1431148159 cc:6d:a0:XX:XX:XX 172.16.16.71 NP-13C1CFXXXXXX *
1431260545 fc:c2:de:XX:XX:XX 172.16.16.121 android-XXXXX2 01:fc:c2:de:XX:XX:XX
1431247578 d8:90:e8:XX:XX:XX 172.16.16.111 android-XXXXX3 01:d8:90:e8:XX:XX:XX
1431236319 00:1a:98:XX:XX:XX 172.16.16.84 android-XXXXX4 *
This gives me 3 relevant bits of data for each active device:

1. The current IP address assigned in the DHCP lease. In my case, all client devices use DHCP, so those are the only ones I'd be interested in for your scenario.
2. The name the device used when querying the DHCP server. All of my Android devices use android-XXXXXX, and my RPis use their hostname (raspiXX), while Rokus use NP-XXXXXXXXXXXX so telling them apart is easy.
3. The device MAC address. This is useful because the 1st 3 bytes of a MAC address designate a manufacturer (although this can be changed, it's not common to do so). My son's iPhone uses a name that I couldn't easily predict, but I can tell that it's an Apple device based on the 1st 3 bytes of the MAC address. I can also pick out my Motorola smartphone:

DC-9B-9C-XX-XX-XX Apple, USA
60-BE-B5-XX-XX-XX Motorola Mobility LLC, USA
FC-C2-DE-XX-XX-XX Murata Manufacturing Co., Ltd., JAPAN
64-66-B3-XX-XX-XX TP-LINK TECHNOLOGIES CO., LTD., CHINA
D8-90-E8-XX-XX-XX Samsung Electronics Co.,Ltd, SOUTH KOREA
00-1A-98-XX-XX-XX Asotel Communication Limited Taiwan Branch, TAIWAN
CC-6D-A0-XX-XX-XX Roku, Inc., USA
00-0D-4B-XX-XX-XX Roku, LLC, USA

dnsmasq can be configured to assign different IP parameters based on any of these patterns. The beauty of doing it on the DHCP server (e.g. RPi) is that I don't have to do any network probes which themselves might look like "human" traffic, and I have access to a lot of device info without having to know about them up front (e.g. building a list of MAC addresses). If I set a short DHCP lease time, for example, for anything requesting a lease with the name android-XXXXXX, the leases disappear when the device is no longer present.

That still just means the phone is in your home though, and certainly not in a specific room or necessarily near a user. My IP subnet can be pinged well out into my yard and on the street. You also have no means of detecting user with a device that's not on your wifi or local network, or who has no device on them.

How localized do you want detection to be? House or room level? Might passive IR sensors be more effective at detecting bodies versus devices? Do you want to know when anybody is present, or only care about certain tech-savvy individuals? What about pets? What if the device goes into standby and is not active on the network for hours?

Interesting addition.
The problem is that my router is the DHCP server and not the RPI

User avatar
bobstro
Posts: 193
Joined: Wed Feb 05, 2014 6:48 am
Location: Central Massachusetts, US
Contact: Website

Re: find mobiles at local network/ router

Fri May 08, 2015 6:21 pm

Canedje wrote:[...] The problem is that my router is the DHCP server and not the RPI
Any particular reason it has to be? I shut down the DHCP server on my router and use my RPi for the greater control it gives me, particularly for DNS. For one thing, every new RPi I boot up that uses DHCP can be pinged directly by hostname with no additional work on the RPi side.

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: find mobiles at local network/ router

Sat May 09, 2015 1:18 pm

bullwinkle wrote:Hi

I did something like this where I scanned for unknown devices on my network. You could modify my stuff to meet your needs
It uses nmap and bash script. Files found here....
http://dumb-looks-free.blogspot.fr/2014 ... vices.html

good luck
I tested this method.
It works fine.
there is one exception.
The Phone of one off my son's is not Always detected if he is still in house
Can there be a setting at the Phone that it only is connetcted to wifi if active doing something with wifi?

User avatar
bobstro
Posts: 193
Joined: Wed Feb 05, 2014 6:48 am
Location: Central Massachusetts, US
Contact: Website

Re: find mobiles at local network/ router

Sat May 09, 2015 2:58 pm

Canedje wrote:[...] The Phone of one off my son's is not Always detected if he is still in house
Can there be a setting at the Phone that it only is connetcted to wifi if active doing something with wifi?
You could try something using a wifi adapter in monitor mode. I am not aware of one that will do this for the RPi, but DL has indicated that adapters based on the RT5370 should work. It sounds like the CreepyDOL project would do much of what you want.

If so, monitor mode will allow you to intercept wifi probes that are commonly, though not always, sent. If you son's wifi is simply off and he's using mobile data, you might not see anything.

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: find mobiles at local network/ router

Sun May 10, 2015 9:17 pm

bobstro wrote:
Canedje wrote:[...] The Phone of one off my son's is not Always detected if he is still in house
Can there be a setting at the Phone that it only is connetcted to wifi if active doing something with wifi?
You could try something using a wifi adapter in monitor mode. I am not aware of one that will do this for the RPi, but DL has indicated that adapters based on the RT5370 should work. It sounds like the CreepyDOL project would do much of what you want.

If so, monitor mode will allow you to intercept wifi probes that are commonly, though not always, sent. If you son's wifi is simply off and he's using mobile data, you might not see anything.
Thanks a good advice.
It looks like nmap is not Always reliable showing all devices.
It also takes about one minute to do a scan. Can this be done faster?

how do I use monitor mode?

I do use this Wifi Dongle:
https://www.sossolutions.nl/raspberry-p ... s/150-mbps

Return to “Python”