0

I need to get the wifi unique ID. SSID is not safe as you can have different wifis with the same name, so I wen to find the MAC address of the router. So far I use arp to get this info but I found a wifi where the MAC address seems to change over time. Is this function the right one to retrieve the MAC adress of the wifi router ?

    def get_wifi_info(self):
        result = subprocess.run(["iwgetid"], capture_output=True, text=True)
        ssid = result.stdout.split('"')[1]

        arp_output = subprocess.check_output(["/usr/sbin/arp", "-a"]).decode("utf-8")
        mac_pattern = re.compile(r"..:..:..:..:..:..")
        mac_address = mac_pattern.findall(arp_output)

        return ssid, mac_address[0]

I am on Raspbian 11. Thanks for your help.

1 Answer 1

0

You can get it using iwconfig with a little proccessing of its output:

try:
    output = subprocess.check_output(["iwconfig"], text=True)
    mac_address = None
    for line in output.splitlines():
        if "HWaddr" in line:
            mac_address = line.split()[1]
            break
    # now you have mac address
except subprocess.CalledProcessError:
    print("Failed to retrieve Wi-Fi MAC address.")
Sign up to request clarification or add additional context in comments.

1 Comment

Thx. I don't get "HWaddr" in the ouput but I get the point. Here is what I get on the MAC adresse line : Mode:Managed Frequency:2.412 GHz Access Point: 00:11:32:50:EA:F3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.