|
@@ -7,8 +7,13 @@ import getmac
|
|
|
|
|
|
__all__ = (
|
|
|
'LocalNetwork',
|
|
|
+ 'is_valid_ipv4',
|
|
|
+ 'enforce_mac',
|
|
|
+ 'enforce_mac',
|
|
|
+ 'parse_ip',
|
|
|
)
|
|
|
|
|
|
+
|
|
|
OUR_MAC = None
|
|
|
OUR_IP = None
|
|
|
OUR_INTERFACE = None
|
|
@@ -53,9 +58,49 @@ class LocalNetwork:
|
|
|
raise RuntimeError('Could not find default interface')
|
|
|
|
|
|
|
|
|
+def is_valid_ipv4(ip: str) -> bool:
|
|
|
+ try:
|
|
|
+ ipaddress.IPv4Address(ip)
|
|
|
+ return True
|
|
|
+ except ipaddress.AddressValueError:
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
+def enforce_mac(mac: str) -> bytes:
|
|
|
+ mac_bytes = []
|
|
|
+ for b in mac.split(':'):
|
|
|
+ mac_bytes.append(int(b, 16))
|
|
|
+ return bytes(mac_bytes)
|
|
|
+
|
|
|
+
|
|
|
+def enforce_ip(ip: str) -> bytes:
|
|
|
+ ip_bytes = []
|
|
|
+ for b in ip.split('.'):
|
|
|
+ ip_bytes.append(int(b))
|
|
|
+ return bytes(ip_bytes)
|
|
|
+
|
|
|
+
|
|
|
+def parse_mac(mac: bytes) -> str:
|
|
|
+ mac_parts = []
|
|
|
+ for b in mac:
|
|
|
+ hex = f'{b:x}'
|
|
|
+ if len(hex) == 1:
|
|
|
+ hex = '0' + hex
|
|
|
+ mac_parts.append(hex)
|
|
|
+ return ':'.join(mac_parts)
|
|
|
+
|
|
|
+
|
|
|
+def parse_ip(ip: bytes) -> str:
|
|
|
+ ip_parts = []
|
|
|
+ for b in ip:
|
|
|
+ ip_parts.append(str(b))
|
|
|
+ return '.'.join(ip_parts)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
foo = LocalNetwork()
|
|
|
- print(foo.get_default_interface())
|
|
|
- print(foo.get_mac())
|
|
|
- print(foo.get_ip())
|
|
|
-# print(getmac.get_mac_address('Ethernet 3'))
|
|
|
+ # print(foo.get_default_interface())
|
|
|
+ # print(foo.get_mac())
|
|
|
+ # print(foo.get_ip())
|
|
|
+ # print(is_valid_ipv4('1.1.1.1'))
|