xref: /freebsd/tests/atf_python/sys/net/tools.py (revision 271171e0)
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            "{} {} -rnW -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_nhops(cls, family: str, fibnum: int = 0):
55        family_key = {"inet": "-4", "inet6": "-6"}.get(family)
56        out = cls.get_output(
57            "{} {} -onW -F {} --libxo json".format(cls.NETSTAT_PATH, family_key, fibnum)
58        )
59        js = json.loads(out)
60        js = js["statistics"]["route-nhop-information"]["nhop-table"]["rt-family"]
61        if js:
62            return js[0]["nh-entry"]
63        else:
64            return []
65
66    @classmethod
67    def get_linklocals(cls):
68        ret = {}
69        ifname = None
70        ips = []
71        for line in cls.get_output(cls.IFCONFIG_PATH).splitlines():
72            if line[0].isalnum():
73                if ifname:
74                    ret[ifname] = ips
75                    ips = []
76                ifname = line.split(":")[0]
77            else:
78                words = line.split()
79                if words[0] == "inet6" and words[1].startswith("fe80"):
80                    # inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
81                    ip = words[1].split("%")[0]
82                    scopeid = int(words[words.index("scopeid") + 1], 16)
83                    ips.append((ip, scopeid))
84        if ifname:
85            ret[ifname] = ips
86        return ret
87