home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
74.06k stars 31.08k forks source link

nmap fails to obtain mac addresses on #123573

Open cometchaser opened 3 months ago

cometchaser commented 3 months ago

The problem

Hi

I wanted to use the Nmap Tracker to monitor my network. However, no devices are found. I am using Homeassistant in version 2024.8.0.

In the logs I only find the note that no MAC address was found for the respective devices:

2024-08-11 10:24:12.506 DEBUG (SyncWorker_0) [homeassistant.components.nmap_tracker] Scanning ['192.168.2.0/24'] with args: -F -T4 --min-rate 10 --host-timeout 15s --exclude 192.168.2.254 --reason -v
2024-08-11 10:24:35.237 DEBUG (SyncWorker_0) [homeassistant.components.nmap_tracker] Finished scanning ['192.168.2.0/24'] with args: -F -T4 --min-rate 10 --host-timeout 15s --exclude 192.168.2.254 --reason -v
2024-08-11 10:24:36.320 INFO (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.2.1
2024-08-11 10:24:37.362 INFO (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.2.3

This problem looked similar to #87623. But this one should be solved.

# python -m getmac -v -dddd --ip 192.168.2.1
DEBUG    Initializing 'ip4' method cache (platform: 'linux')
DEBUG    27 methods available: CtypesHost, ArpFile, ArpingHost, SysIfaceFile, FcntlIface, UuidLanscan, GetmacExe, IpconfigExe, WmicExe, ArpExe, DarwinNetworksetupIface, ArpFreebsd, ArpOpenbsd, IfconfigWithIfaceArg, IfconfigEther, IfconfigOther, IpLinkIface, NetstatIface, IpNeighborShow, ArpVariousArgs, UuidArpGetNode, DefaultIfaceLinuxRouteFile, DefaultIfaceIpRoute, DefaultIfaceRouteCommand, DefaultIfaceRouteGetCommand, DefaultIfaceOpenBsd, DefaultIfaceFreeBsd
DEBUG    9 type-filtered methods for 'ip4': CtypesHost, ArpFile, ArpingHost, ArpExe, ArpFreebsd, ArpOpenbsd, IpNeighborShow, ArpVariousArgs, UuidArpGetNode
DEBUG    5 platform-filtered methods for 'linux' (method_type='ip4'): ArpFile, ArpingHost, IpNeighborShow, ArpVariousArgs, UuidArpGetNode
DEBUG    5 tested methods for 'ip4': ArpFile, ArpingHost, IpNeighborShow, ArpVariousArgs, UuidArpGetNode
DEBUG    Current method cache: {'ip4': 'ArpFile', 'ip6': 'None', 'iface': 'None', 'default_iface': 'None'}
DEBUG    Current fallback cache: {'ip4': '[<getmac.getmac.ArpingHost object at 0x7f92673611c0>, <getmac.getmac.IpNeighborShow object at 0x7f9267171010>, <getmac.getmac.ArpVariousArgs object at 0x7f926706b950>, <getmac.getmac.UuidArpGetNode object at 0x7f926706ac30>]', 'ip6': '[]', 'iface': '[]', 'default_iface': '[]'}
DEBUG    Finished initializing 'ip4' method cache
DEBUG    Attempting get() (method='ArpFile', method_type='ip4', arg='192.168.2.1')
DEBUG    Not sending UDP packet, using network request method 'ArpingHost' instead
DEBUG    Attempting get() (method='ArpingHost', method_type='ip4', arg='192.168.2.1')
DEBUG    Running: '/usr/sbin/arping -f -c 1 192.168.2.1'
DEBUG    Falling back to Habets arping
DEBUG    Running: '/usr/sbin/arping -r -C 1 -c 1 192.168.2.1'
DEBUG    Method 'ArpingHost' failed for 'ip4' lookup
DEBUG    Raw MAC found: None
DEBUG    getmac took 1.0358 seconds

I tried to fix my problem by following https://github.com/home-assistant/core/issues/87623#issuecomment-1422701870 and add

import getmac
from getmac import get_mac_address
getmac.getmac.FORCE_METHOD="ArpFile"

to /usr/src/homeassistant/homeassistant/components/nmap_tracker/__init__.py. But it doesn't fix to problem. Also the installation of apk add arping doesn't fix the issue.

What version of Home Assistant Core has the issue?

core-2024.8.0

What was the last working version of Home Assistant Core?

core-2024.8.0

What type of installation are you running?

Home Assistant Container

Integration causing the issue

nmap tracker

Link to integration documentation on our website

https://www.home-assistant.io/integrations/nmap_tracker/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 3 months ago

nmap_tracker documentation nmap_tracker source

carefulcomputer commented 2 months ago

same here.

silvio-l commented 2 months ago

Hi, i have this problem too. (Home Assistant OS with Core 2024.9.2). NMAP finds the right IPs but can not get the mac's of the devices and fails.

2024-09-23 12:54:47.754 DEBUG (SyncWorker_7) [homeassistant.components.nmap_tracker] Scanning ['192.168.50.0/24'] with args: -sn -PE --exclude 192.168.40.50 --reason -v
2024-09-23 12:54:49.159 DEBUG (SyncWorker_7) [homeassistant.components.nmap_tracker] Finished scanning ['192.168.50.0/24'] with args: -sn -PE --exclude 192.168.40.50 --reason -v
2024-09-23 12:54:49.180 INFO (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.50.1
2024-09-23 12:54:49.198 INFO (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.50.50
2024-09-23 12:54:49.218 INFO (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.50.88
2024-09-23 12:54:49.235 INFO (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.50.140
2024-09-23 12:54:49.259 INFO (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.50.141
2024-09-23 12:54:49.281 INFO (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.50.225
cometchaser commented 1 month ago

So, It updated to 2024.10 and still face the problem:

024-10-04 10:19:06.085 DEBUG (SyncWorker_13) [homeassistant.components.nmap_tracker] Scanning ['192.168.2.0/24'] with args: -F -T4 --min-rate 10 --host-timeout 15s --exclude 192.168.2.254 --reason -v
2024-10-04 10:19:12.965 DEBUG (SyncWorker_13) [homeassistant.components.nmap_tracker] Finished scanning ['192.168.2.0/24'] with args: -F -T4 --min-rate 10 --host-timeout 15s --exclude 192.168.2.254 --reason -v
2024-10-04 10:19:13.979 WARNING (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.2.1
2024-10-04 10:19:14.989 WARNING (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.2.3
2024-10-04 10:19:16.008 WARNING (MainThread) [homeassistant.components.nmap_tracker] No MAC address found for 192.168.2.10

Any ideas?

stickpin commented 1 month ago

I had the same issue and I've just added missing mac addresses to the arp table manually:

arp -v -i <eth_interface> -s <hostname> <mac_address>

not ideal, but solves the issue.

carefulcomputer commented 1 month ago

@stickpin , can you please share which file/line you changed. This will help other folks who are running into same issue.

stickpin commented 1 month ago

It's not a file. just a regular ssh to the Home Assistant instance. Not sure what is your setup is and if this workaround applies to you, but in my case all the IPs that I monitor with Nmap are static, so I've just created a file arp_table and run the following command:

arp -v -i eth0 -f arp_table - replace eth0 with your interface

arp_table file format is: <hostname> <mac address>

for example:

mycomputer.local 44:33:32:22:11:22
myphone.local 22:33:32:22:11:44
mytablet.local 11:33:32:22:11:33

I am planning to run it with init.d every time the system boots and it will do the trick.

And here is init.d update_arp_table script:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          update-arp-table
# Required-Start:    $network
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Load ARP table at boot
# Description:       Loads ARP table from /root/arp_table to eth0 interface on boot.
### END INIT INFO

# Path to the ARP table file
ARP_TABLE="/root/arp_table"

# Define the start function
start() {
    echo "Loading ARP table to eth0 from $ARP_TABLE"
    /usr/sbin/arp -v -i eth0 -f $ARP_TABLE
}

# Define the stop function (not required but placeholder)
stop() {
    echo "Stopping arp_script (no action required)"
}

# Parse the command line argument
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: /etc/init.d/arp_script {start|stop|restart}"
    exit 1
    ;;
esac

exit 0