From 4cadb91a2ce7b14c6fed34cea82ed2736f871b6e Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Sun, 8 Oct 2023 15:12:42 -0400 Subject: [PATCH] fixed netif info, added connections --- src/hetrixtools_freebsd/cli.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/hetrixtools_freebsd/cli.py b/src/hetrixtools_freebsd/cli.py index 3a592d9..db79986 100644 --- a/src/hetrixtools_freebsd/cli.py +++ b/src/hetrixtools_freebsd/cli.py @@ -161,13 +161,34 @@ class Update: result = [] for ifname in t0list: - result.append("{},{},{};".format(ifname, t1[ifname][0]-t0[ifname][0], - t1[ifname][1]-t0[ifname][1])) + result.append("{},{},{};".format(ifname, t1list[ifname][0]-t0list[ifname][0], + t1list[ifname][1]-t0list[ifname][1])) - self.nics = result - self.ipv4 = "" # getNetInfo - self.ipv6 = "" # getNetInfo - self.conn = "" # getNetInfo + self.nics = base64.b64encode("".join(result).encode()).decode('utf-8') + + # Now get the IPv4 and IPv6 addresses for each interface + ifconfigtxt = self.callCommand(command=['netstat', '-a']) + ifconfig = jc.parse(parser_mod_name="ifconfig", data=ifconfigtxt) + # XXX only returns first IP address assigned to an interface. Interfaces cnan have more + # than one IP + # Deliberately excludes lo0 interface + ipv4 = {ifname['name']:ifname['ipv4'][0]['address'] for ifname in ifconfig + if ifname['name'] != 'lo0' } + self.ipv4 = ','.join(f"{key}:{value}" for key, value in ipv4.items()) + + # XXX Same thing: only catches first ipv6 addr, which is probably worse, since + # most ipv6 interfaces have several IP addresses. + ipv6 = {ifname['name']:ifname['ipv6'][0]['address'] for ifname in ifconfig + if ifname['name'] != 'lo0' } + self.ipv6 = ','.join(f"{key}:{value}" for key, value in ipv6.items()) + + # Get open connections + conntext = self.callCommand(['sockstat', '-c', '-n', '-P', 'tcp']) + # At first glance it might seem like a bug. len() returns the number of lines + # counting from 0, so if there are 4 lines it reports 3. But one of the lines + # is a header line, not a connection. So we WANT to return 1 less than the actual + # number of lines in the output. + self.conn = str(len(conntext.split('\n'))) def getSysCtlInfo(self): """ Parses all the things that can be gotten from sysctl and sets all the