fixed netif info, added connections

main
Paco Hope 2023-10-08 15:12:42 -04:00
parent 7b1d74afad
commit 4cadb91a2c
1 changed files with 27 additions and 6 deletions

View File

@ -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