selfprivacy-rest-api/selfprivacy_api/utils/network.py

29 lines
725 B
Python
Raw Normal View History

2022-07-05 05:14:37 +00:00
#!/usr/bin/env python3
"""Network utils"""
import subprocess
import re
2022-07-07 13:53:19 +00:00
2022-07-05 05:14:37 +00:00
def get_ip4():
"""Get IPv4 address"""
try:
2022-07-07 13:53:19 +00:00
ip4 = subprocess.check_output(["ip", "addr", "show", "dev", "eth0"]).decode(
"utf-8"
)
2022-07-05 05:14:37 +00:00
ip4 = re.search(r"inet (\d+\.\d+\.\d+\.\d+)\/\d+", ip4)
except subprocess.CalledProcessError:
ip4 = None
return ip4.group(1) if ip4 else None
2022-07-07 13:53:19 +00:00
2022-07-05 05:14:37 +00:00
def get_ip6():
"""Get IPv6 address"""
try:
2022-07-07 13:53:19 +00:00
ip6 = subprocess.check_output(["ip", "addr", "show", "dev", "eth0"]).decode(
"utf-8"
)
2022-07-05 05:14:37 +00:00
ip6 = re.search(r"inet6 (\S+)\/\d+", ip6)
except subprocess.CalledProcessError:
ip6 = None
return ip6.group(1) if ip6 else None