Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_mac_address returns None if there are no routes on the Linux system #78

Open
vlstill opened this issue Jan 6, 2023 · 8 comments
Open
Assignees
Labels

Comments

@vlstill
Copy link

vlstill commented Jan 6, 2023

Describe the bug
If there are no routes on the system, the getmac.get_mac_address() returns None even if there are network interfaces.

This can happen e.g. in testing environments such as virtual machines and or inside Linux network namespaces, but in general in systems not connected to larger networks.

To Reproduce

Tested on Ubuntu 20.04LTS (under root)

$ ip netns add getmac-test
$ ip netns exec getmac-test ip link set dev lo up
$ ip netns exec getmac-test python
>>> import getmac
>>> getmac.get_mac_address() is None
True
>>> exit()
$ ip netns exec getmac-test ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

Expected behavior
A mac address is returned even without routes set.

System info
(please complete the following information):

  • OS name: Ubuntu 20.04LTS
  • Python version: 3.8.10

Additional context

none

@GhostofGoes
Copy link
Owner

Hey Vladimír, thanks for the bug report. Would you mind re-running the same test, with debugging output enabled?

It should be something like this (apologies if it's incorrect syntax, I'm not very familiar with the semantics of ip netns exec):

ip netns exec getmac-test python -m getmac -v -dddd

Also, try the same, with the interface specified manually. This shouldn't be necessary, but I'd like to see if it's at least able to get a MAC.

ip netns exec getmac-test python -m getmac -v -dddd -i lo

@GhostofGoes GhostofGoes self-assigned this Jan 11, 2023
@vlstill
Copy link
Author

vlstill commented Jan 19, 2023

Hi!

The log with debug info: getmac.log

With explicit loopback, it works:

$ ip netns exec getmac-test python -m getmac -v -dddd -i lo
DEBUG    Trying: '_read_sys_iface_file' (to_find: 'lo')
DEBUG    Result: 00:00:00:00:00:00


DEBUG    Raw MAC found: 00:00:00:00:00:00

00:00:00:00:00:00

@GhostofGoes
Copy link
Owner

Thanks for the log file. Yeah, this is definitely a bug. If no routes are found, the rational behavior would be to fallback to the first non-loopback interface, and if no interfaces are found, then fallback to the loopback interface, if it exists. The program "knows" it exists based on output of ip addr, so it should be selecting it, but isn't.

This may be addressed by the refactor, I'll add a regression test and make sure it is. In the meantime, you can try using the beta release of the refactor:

pip install --pre -U getmac

GhostofGoes added a commit that referenced this issue Jan 22, 2023
Properly fixing this will take some significant work to build a state of the interfaces on the system.
I'm deferring that work to 1.0.0 in the interests of getting 0.9.0 out the door.
@GhostofGoes
Copy link
Owner

Alright, so turns out refactor branch had the same behavior. I partially addressed the issue in this commit: c2e54e5

Due to how getmac works internally, a proper fix will take a significant amount of work. In the interests of getting 0.9.0 released I'm deferring that work to 1.0.0.

@GhostofGoes
Copy link
Owner

0.9.0 is released that partially fixes this issue

@mark007
Copy link

mark007 commented Feb 8, 2023

Hi. Various integrations within the homeassistant GitHub project seem to be having issues in its most recent versions, where integrations that use getmac to get the Mac address, are returning None. I wonder is it related to this issue. Forcing the method seems to work as a workaround but it feels wrong to have to force the method.

home-assistant/core#87623

home-assistant/core#87146

@GhostofGoes
Copy link
Owner

GhostofGoes commented Feb 9, 2023

@mark007 Would you mind opening a new issue to track those issues? I think I know the cause and might be able to do a quick fix.

@mark007
Copy link

mark007 commented Feb 9, 2023

No problem, created here. Thanks a lot.

#83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants