mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-27 06:21:28 +00:00
7935de0fe1
Co-authored-by: inexcode <inex.code@selfprivacy.org> Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api/pulls/13
30 lines
763 B
Python
30 lines
763 B
Python
#!/usr/bin/env python3
|
|
"""Network utils"""
|
|
import subprocess
|
|
import re
|
|
from typing import Optional
|
|
|
|
|
|
def get_ip4() -> str:
|
|
"""Get IPv4 address"""
|
|
try:
|
|
ip4 = subprocess.check_output(["ip", "addr", "show", "dev", "eth0"]).decode(
|
|
"utf-8"
|
|
)
|
|
ip4 = re.search(r"inet (\d+\.\d+\.\d+\.\d+)\/\d+", ip4)
|
|
except subprocess.CalledProcessError:
|
|
ip4 = None
|
|
return ip4.group(1) if ip4 else ""
|
|
|
|
|
|
def get_ip6() -> str:
|
|
"""Get IPv6 address"""
|
|
try:
|
|
ip6 = subprocess.check_output(["ip", "addr", "show", "dev", "eth0"]).decode(
|
|
"utf-8"
|
|
)
|
|
ip6 = re.search(r"inet6 (\S+)\/\d+", ip6)
|
|
except subprocess.CalledProcessError:
|
|
ip6 = None
|
|
return ip6.group(1) if ip6 else ""
|