From 83435324317e9175a88ea6a8713611a0e5c6f581 Mon Sep 17 00:00:00 2001 From: pdesanex Date: Thu, 8 Aug 2024 11:43:24 +0100 Subject: [PATCH] add get_mac() --- CHANGELOG.md | 8 +++++++- iblutil/__init__.py | 2 +- iblutil/io/net/base.py | 15 +++++++++++++++ tests/test_net.py | 6 ++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 760a6ad..2c22f00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,13 @@ * Minor releases (X.1.X) are new features such as added functions or small changes that don't cause major compatibility issues. * Major releases (1.X.X) are major new features or changes that break backward compatibility in a big way. -## [Latest](https://github.com/int-brain-lab/iblutil/commits/main) [1.11.0] +## [Latest](https://github.com/int-brain-lab/iblutil/commits/main) [1.12.0] + +### Added + +- io.net.base.get_mac: function returning the machine's unique MAC address formatted according to IEEE 802 specifications. + +## [1.11.0] ### Added diff --git a/iblutil/__init__.py b/iblutil/__init__.py index da77e85..666b2f7 100644 --- a/iblutil/__init__.py +++ b/iblutil/__init__.py @@ -1 +1 @@ -__version__ = '1.11.0' +__version__ = '1.12.0' diff --git a/iblutil/io/net/base.py b/iblutil/io/net/base.py index 2ef1d49..69aad14 100644 --- a/iblutil/io/net/base.py +++ b/iblutil/io/net/base.py @@ -2,6 +2,7 @@ import re import json import socket +import uuid import warnings import logging from asyncio import isfuture @@ -36,6 +37,20 @@ def external_ip(): return ipaddress.ip_address(urllib.request.urlopen('https://ident.me').read().decode('utf8')) +def get_mac() -> str: + """ + Fetch the machine's unique MAC address formatted according to IEEE 802 specifications. + + Returns + ------- + str + The MAC address of the device formatted in six groups of two + hexadecimal digits separated by hyphens in transmission order + (e.g., 'BA-DB-AD-C0-FF-EE'). + """ + return uuid.getnode().to_bytes(6, 'big').hex('-').upper() + + def is_valid_ip(ip_address) -> bool: """ Test whether IP address is valid. diff --git a/tests/test_net.py b/tests/test_net.py index 5c01758..77f1c96 100644 --- a/tests/test_net.py +++ b/tests/test_net.py @@ -1,3 +1,4 @@ +from unittest.mock import patch import sys import asyncio import logging @@ -9,6 +10,7 @@ from datetime import date from iblutil.io.net import base, app +from iblutil.io.net.base import get_mac ver = (getattr(sys.version_info, v) for v in ('major', 'minor', 'micro')) ver = Version('.'.join(map(str, ver))) @@ -49,6 +51,10 @@ def test_external_ip(self): """Test for external_ip""" self.assertFalse(ipaddress.ip_address(base.external_ip()).is_private) + def test_get_mac(self): + with patch('iblutil.io.net.base.uuid.getnode', return_value=205452675710958): + self.assertEqual(get_mac(), 'BA-DB-AD-C0-FF-EE') + def test_ExpMessage(self): """Test for ExpMessage.validate method.""" # Check identity