network_scanner icon indicating copy to clipboard operation
network_scanner copied to clipboard

0 devices

Open Jab2870 opened this issue 1 year ago • 47 comments

For some reason, the integration does not show any devices.

image

image

Nmap is installed and working in the HA docker container, and scanning the specified subnet shows a number of devices:

image

The only log message I see is that it is taking more than 10 seconds:

Logger: homeassistant.components.sensor
Source: runner.py:189
integration: Sensor (documentation, issues)
First occurred: 16:45:25 (1 occurrences)
Last logged: 16:45:25

Setup of sensor platform network_scanner is taking over 10 seconds.

However, after leaving the intergration for well over 20 mins it sill shows 0

Jab2870 avatar Sep 17 '24 15:09 Jab2870

could you help confirm if the version you are using is 1.0.7. This plugin is now available on HACS as well, you can get it up to date through it. https://hacs.xyz/

parvez avatar Sep 18 '24 12:09 parvez

I can confirm i'm using 1.0.7 and installed it through hacs

Jab2870 avatar Sep 18 '24 14:09 Jab2870

i faced similar issue when my sophos firewall was blocking the scan requests. could you help check if you have any firewall that's blocking, or create a firewall rule to allow.

parvez avatar Sep 19 '24 08:09 parvez

Good morning ! Can anyone help me? I have the same problem.. Let me explain: I have a local network in 10.0.0.x, and with a VPN, we reach an extent of 120 range, of 10.11.x.x (example: 10.11.47.2, 10.11.95.2, etc.) I can ping them using ssh, but nothing is found when scanning when I put 10.11.47.0/24.. Any idea?

AlexH0606 avatar Sep 19 '24 14:09 AlexH0606

i faced similar issue when my sophos firewall was blocking the scan requests. could you help check if you have any firewall that's blocking, or create a firewall rule to allow.

I don't believe so. As I demonstrated in the initial post, I was able to run a ping scan from the home assistant docker container to the range I am trying to scan.

Jab2870 avatar Sep 19 '24 15:09 Jab2870

Could you enable debug log and see anything unusual?

parvez avatar Sep 19 '24 23:09 parvez

the relevant bits (took out bits relating to other intergrations

2024-09-20 11:05:14.860 INFO (MainThread) [custom_components.network_scanner.sensor] Network Scanner initialized
2024-09-20 11:05:14.860 DEBUG (MainThread) [custom_components.network_scanner.sensor] Scanning network
2024-09-20 11:05:24.848 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform network_scanner is taking over 10 seconds.
2024-09-20 11:10:03.268 DEBUG (MainThread) [custom_components.network_scanner.sensor] ip_range: 192.168.0.0/24
2024-09-20 11:10:03.268 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_1:
2024-09-20 11:10:03.268 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_2:
2024-09-20 11:10:03.268 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_3:
2024-09-20 11:10:03.268 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_4:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_5:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_6:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_7:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_8:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_9:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_10:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_11:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_12:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_13:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_14:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_15:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_16:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_17:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_18:
2024-09-20 11:10:03.269 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_19:
2024-09-20 11:10:03.270 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_20:
2024-09-20 11:10:03.270 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_21:
2024-09-20 11:10:03.270 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_22:
2024-09-20 11:10:03.270 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_23:
2024-09-20 11:10:03.270 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_24:
2024-09-20 11:10:03.270 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping_25:
2024-09-20 11:10:03.270 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mappings:
2024-09-20 11:10:03.270 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping unparsed:
2024-09-20 11:10:03.270 DEBUG (MainThread) [custom_components.network_scanner.sensor] mac_mapping parsed:
2024-09-20 11:10:03.275 INFO (MainThread) [custom_components.network_scanner.sensor] Network Scanner initialized
2024-09-20 11:10:03.275 DEBUG (MainThread) [custom_components.network_scanner.sensor] Scanning network
2024-09-20 11:10:13.269 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform network_scanner is taking over 10 seconds.

Jab2870 avatar Sep 20 '24 10:09 Jab2870

so, I've done a bit of experimenting. It looks like hosts are dropped if they don't contain a mac address in the nmap scan. Nmap is only able to get the mac address for a device if it's on the same subnet. To demonstrate I added lines 96 and 97 to the sensor.py file:

image

The result was image

Jab2870 avatar Sep 20 '24 10:09 Jab2870

It's because nmap runned in unprivileged mode. I added caps to nmap bin:

sudo setcap cap_net_raw,cap_net_admin,cap_net_bind_service+eip $(which nmap)

And set NMAP_PRIVILEGED=1

rkakrik avatar Sep 24 '24 09:09 rkakrik

I still don't think nmap will be able to get the mac address of a computer in a different subnet. Even with privialaged mode. Just run it as root and still no mac address given

Jab2870 avatar Sep 24 '24 09:09 Jab2870

To find the difference use two commands:

  1. nmap -sn 192.168.2.0/24
  2. nmap --privileged -sn 192.168.2.0/24 The first command do not provide MAC address, but the second does. In ~/.homeassistant/custom_components/network_scanner/sensor.py on the 74 line there is calling for nmap command with parameters -sn self.nm.scan(hosts=self.ip_range, arguments='--privileged -sn')

That`s why you see no hosts.

If you change parameters for --privileged -sn self.nm.scan(hosts=self.ip_range, arguments='--privileged -sn')

Your network scanner will see all of your hosts in LAN.

bialeta avatar Oct 03 '24 20:10 bialeta

I also get 0 devices, scan does not do anything :

2024-12-17 13:02:44.037 INFO (MainThread) [custom_components.network_scanner.sensor] Network Scanner initialized
2024-12-17 13:02:44.037 DEBUG (MainThread) [custom_components.network_scanner.sensor] Scanning network

HA is running on Docker in host network mode, nmap is installed and working in the container and the scans give MAC addresses wether nmap runs with or without --privileged. I don't know what I can do to help debug ?

Mpic avatar Dec 17 '24 13:12 Mpic

Sad to report, I had the same issue. After hours of troubleshooting, the integration did not find any devices even though I could EXEC into the HA container and run the NMAP command and find all my hosts in less than 3 seconds.

ppastur avatar Jan 14 '25 08:01 ppastur

I doesn't work from within Docker. Also the privileged mode doesn't work if you are not using the host network/adapter. If you are on a separate networking range, mac addresses won't be available.

I've just created a pull request (https://github.com/parvez/network_scanner/pull/27) with some changes:

  1. Added a configuration option to enable privilege mode
  2. Allow the sensor to show devices if no mac address is available. This will limit functionality, but will show the amount of devices on the network.

dib0 avatar Jan 16 '25 16:01 dib0

Great - happy to test in my HA here if there's a way to pull it down

ajlennon avatar Jan 16 '25 16:01 ajlennon

I doesn't work from within Docker. Also the privileged mode doesn't work if you are not using the host network/adapter. If you are on a separate networking range, mac addresses won't be available.

I've just created a pull request (#27) with some changes:

  1. Added a configuration option to enable privilege mode
  2. Allow the sensor to show devices if no mac address is available. This will limit functionality, but will show the amount of devices on the network.

Hmmmm, perhaps my knowledge is lacking here but I manually added privileged mode to HA container via portainer and my HA container's ethernet card is on the host network i.e. the same IP range as the host and all my other devices and lastly why can I successfully run NMAP in a terminal session (EXEC'd into HA container) and return all network devices? Please don't think I am debating your comment; I am just trying to understand.

ppastur avatar Jan 17 '25 04:01 ppastur

@ppastur No you're right. The difference here is that you are running the container on the host network. If you are on the same ip-subnet as the rest of the network, the mac-addresses will be visible to you. I'm running docker hosts on several subnets, but try to avoid using the docker host network when possible (have multiple containers running and try to separate then in to relevant subnets). If your network card is on a different subnet mac-addresses won't be visible. Simply put: those are strictly for the same subnet, outside of it routing is required and with routing this kind of information gets "filtered"/irrelevant.

@ajlennon You can download the changes from https://github.com/dib0/network_scanner/ and manually replace them in the custom_components dir of Home Assistant (make sure to delete the pycache subdirectory). With that you are able to test the incoming PR changes.

dib0 avatar Jan 17 '25 05:01 dib0

@dib0 I understand there may be issues with hostnames in the scenario you describe. I think I'm not explaining myself very well. I'll try again, I want this to work;

  • The subnet I am trying to scan is 192.168.1.0/24
  • My HA container has an address of 192.168.1.200
  • The Docker Host (hosting HA) has an address of 192.168.1.100

When I add the integration, (HA set up to run in privileged mode) and configure it to scan 192.168.1.0/24 (also tried 192.168.1.0-254) I get 0 devices returned.

I then exec into HA and run ;

  1. nmap -sn 192.168.1.0/24 result: Nmap done: 256 IP addresses (28 hosts up) scanned in 9.34 seconds and both IP and MAC addresses are displayed
  2. nmap --privileged -sn 192.168.1.0/24 result: Nmap done: 256 IP addresses (27 hosts up) scanned in 5.78 seconds and both IP and MAC addresses are displayed

so as you can see, from a networking perspective, HA can see all these devices and this is why I am confused with you comment. Thank you for persisting with my question, I'm sorry if I am being obtuse

ppastur avatar Jan 17 '25 06:01 ppastur

Hi @ppastur ,

I'm sorry I misunderstood your previous remark/question. The setup you have done should work indeed. Since the container has an ip-address on the local network mac addresses should show up.

The command issued is: nmap -oX - -sn {ip} Or with the changed version nmap -oX - --privileged -sn {ip}

I tested it after moving my container to the host network and the command returns mac addresses as well. The network scanning sensor also shows networking devices (including mac addresses in the attributes).

I'm not sure why it isn't working for you. I must say that I only tested the version of the custom component I've changed (and is in the PR), so there might be a difference between the versions.

dib0 avatar Jan 17 '25 10:01 dib0

Hi @dib0 , I copied over the files from the PR and restarted HA. On restart, I finally get devices discovered however there are no hostnames or MACs.

Image

The log only shows the following;

Image

I ran a terminal session to the host and EXEC'd into HA and ran: nmap -oX - --privileged -sn 192.168.1.0/24 and it returned all my devices with rich information such as MAC, Vendor etc

Thank you for your work so far - what do you think could be the next step in the troubleshooting process?

ppastur avatar Jan 17 '25 15:01 ppastur

You're welcome,

The binary sensor is a new sensor I've just added and am currently testing. It will be true if previously unknown devices are found (until the next scan that is). Since I want to setup a notification on new devices on the network.

Regarding the missing MAC addresses: is the network card in the container bridged? Currently I've setup the Home Assistant container to use the host network device, so it has the same ip-address as the host. Maybe that there is still a difference in the network being bridged or shared with the host.

Image Like this is is working, including mac-addresses.

dib0 avatar Jan 17 '25 15:01 dib0

My container network looks like this:

Image

It is a bridged network (QNAP NAS calls them virtual switch).

What really bugs me is that the NMAP command you shared with me works as expected in the container terminal so I cant see how the network setup is the problem. If it was then the terminal command would fail as well. The other point is that the only reason I can see IP addresses right now is due to your updated code - nothing else has changed.

ppastur avatar Jan 18 '25 07:01 ppastur

Hmmm...

I'm running Docker on a Synology NAS, but that shouldn't make any difference. Not sure why it goes wrong. If the command shows mac addresses from the commandline and the altered version shows devices the nmap command is issued and data is processed.

The only thing I can think of is adding a lot more debug logging and see if anything pops up from the logs. It's probably not an error or something, but anything that shows whatever is happening here.

If you want me to I can add more debug logging.

dib0 avatar Jan 18 '25 10:01 dib0

That would be amazing!! Yes please

ppastur avatar Jan 18 '25 12:01 ppastur

Quick question while you are doing this - I pop a load of smart devices onto my network here and then have to go to my router to get the IP address (although yes sometimes I could use mDNS or even go to the pro-level ARP manual setup if I know the device MAC address).

But basically I'd like to be able to go to the page on HA and see the new device.

So I know it scans repeatedly but is there a feature - or could a feature be added - to initiate a scan manually?

ajlennon avatar Jan 18 '25 13:01 ajlennon

@ppastur Alright, I'll try to do that later today.

@ajlennon That would probably not be so hard, I guess. I'm thinking of adding a service to scan immediately, so you can add a helper button or something like that to start the scan (scans are running every 15 minutes at the moment).

And maybe a binary sensor indicating a scan is currently running or not?

dib0 avatar Jan 19 '25 06:01 dib0

And maybe a binary sensor indicating a scan is currently running or not?

Sounds awesome :-D

ajlennon avatar Jan 19 '25 10:01 ajlennon

Stretch goal - this is probably a ridiculous overreach but might be fun.

What about listening for DHCP broadcasts and logging the info when an IP is requested and assigned :-D

ajlennon avatar Jan 19 '25 10:01 ajlennon

Alright I've made some changes in my repo (https://github.com/dib0/network_scanner/tree/main):

  1. Added the option to reconfigure a configured scanner
  2. Fixes for the binary sensor that indicates if new devices were found
  3. Changes nmap scanning parameters to include -PR and added a basic ARP scanning technique to try to find missing MAC addresses
  4. Added a binary sensor indication if a scan is currently running
  5. Adding a service (network_scanner.start_scan) to manually initiate a scan
  6. Added far more logging for debugging purposes.

I'm currently testing the changes. Feel free to download the files and give them a go (and let me know if there are any problems)

dib0 avatar Jan 19 '25 15:01 dib0

Will do now!

ajlennon avatar Jan 19 '25 15:01 ajlennon