xref: /freebsd/tests/atf_python/sys/net/tools.py (revision 90e29718cffcec987769ccbe39308357202c46d5)
1#!/usr/local/bin/python3
2import json
3import os
4import socket
5import time
6from ctypes import cdll
7from ctypes import get_errno
8from ctypes.util import find_library
9from typing import List
10from typing import Optional
11
12
13class ToolsHelper(object):
14    NETSTAT_PATH = "/usr/bin/netstat"
15    IFCONFIG_PATH = "/sbin/ifconfig"
16
17    @classmethod
18    def get_output(cls, cmd: str, verbose=False) -> str:
19        if verbose:
20            print("run: '{}'".format(cmd))
21        return os.popen(cmd).read()
22
23    @classmethod
24    def print_output(cls, cmd: str, verbose=True):
25        if verbose:
26            print("======= {} =====".format(cmd))
27        print(cls.get_output(cmd))
28        if verbose:
29            print()
30
31    @classmethod
32    def print_net_debug(cls):
33        cls.print_output("ifconfig")
34        cls.print_output("netstat -rnW")
35
36    @classmethod
37    def set_sysctl(cls, oid, val):
38        cls.get_output("sysctl {}={}".format(oid, val))
39
40    @classmethod
41    def get_routes(cls, family: str, fibnum: int = 0):
42        family_key = {"inet": "-4", "inet6": "-6"}.get(family)
43        out = cls.get_output(
44            "{} {} -rn -F {} --libxo json".format(cls.NETSTAT_PATH, family_key, fibnum)
45        )
46        js = json.loads(out)
47        js = js["statistics"]["route-information"]["route-table"]["rt-family"]
48        if js:
49            return js[0]["rt-entry"]
50        else:
51            return []
52
53    @classmethod
54    def get_linklocals(cls):
55        ret = {}
56        ifname = None
57        ips = []
58        for line in cls.get_output(cls.IFCONFIG_PATH).splitlines():
59            if line[0].isalnum():
60                if ifname:
61                    ret[ifname] = ips
62                    ips = []
63                ifname = line.split(":")[0]
64            else:
65                words = line.split()
66                if words[0] == "inet6" and words[1].startswith("fe80"):
67                    # inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
68                    ip = words[1].split("%")[0]
69                    scopeid = int(words[words.index("scopeid") + 1], 16)
70                    ips.append((ip, scopeid))
71        if ifname:
72            ret[ifname] = ips
73        return ret
74