From fadbe30392bf216c9df1eb8203bb2ab14d8567d2 Mon Sep 17 00:00:00 2001 From: Stephen M Moraco Date: Mon, 17 Aug 2020 13:50:29 -0600 Subject: [PATCH] update mech to ignore HA network devices --- ChangeLog | 5 +++ ISP-RPi-mqtt-daemon.py | 94 ++++++++++++++++++++++++++++++++++++------ Release | 4 +- 3 files changed, 89 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9b6cb41..c90eefe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Aug 17 19:46:44 2020 -0600 v1.5.1 + + - Filter ignoring HA network devices was incomplete - fixed + - Possibly breaking change (UniqueID may change for some users) + Mon Aug 17 01:26:21 2020 -0600 v1.5.0 - Added Memory Reporting diff --git a/ISP-RPi-mqtt-daemon.py b/ISP-RPi-mqtt-daemon.py index 71336b8..71e524f 100755 --- a/ISP-RPi-mqtt-daemon.py +++ b/ISP-RPi-mqtt-daemon.py @@ -25,7 +25,7 @@ from signal import signal, SIGPIPE, SIG_DFL signal(SIGPIPE,SIG_DFL) -script_version = "1.5.0" +script_version = "1.5.1" script_name = 'ISP-RPi-mqtt-daemon.py' script_info = '{} v{}'.format(script_name, script_version) project_name = 'RPi Reporter MQTT2HA Daemon' @@ -400,10 +400,40 @@ def getUptime(): rpi_uptime = rpi_uptime_raw.replace(timeStamp, '').lstrip().replace('up ', '') print_line('rpi_uptime=[{}]'.format(rpi_uptime), debug=True) -def getNetworkIFs(): - global rpi_interfaces - global rpi_mac - out = subprocess.Popen('/sbin/ifconfig | /bin/egrep "Link|flags|inet |ether " | /bin/egrep -v -i "lo:|loopback|inet6|\:\:1|127\.0\.0\.1"', +def getNetworkIFsUsingIP(ip_cmd): + cmd_str = '{} link show | /bin/egrep -v "noque|link"'.format(ip_cmd) + out = subprocess.Popen(cmd_str, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + stdout,_ = out.communicate() + lines = stdout.decode('utf-8').split("\n") + interfaceNames = [] + line_count = len(lines) + if line_count > 2: + line_count = 2; + if line_count == 0: + print_line('ERROR no lines left by ip(8) filter!',error=True) + sys.exit(1) + + for lineIdx in range(line_count): + trimmedLine = lines[lineIdx].lstrip().rstrip() + lineParts = trimmedLine.split() + interfaceName = lineParts[1].replace(':', '') + interfaceNames.append(interfaceName) + print_line('interfaceNames=[{}]'.format(interfaceNames), debug=True) + + trimmedLines = [] + for interface in interfaceNames: + lines = getSingleInterfaceDetails(interface) + for currLine in lines: + trimmedLines.append(currLine) + + loadNetworkIFDetailsFromLines(trimmedLines) + +def getSingleInterfaceDetails(interfaceName): + cmdString = '/sbin/ifconfig {} | /bin/egrep "Link|flags|inet |ether " | /bin/egrep -v -i "lo:|loopback|inet6|\:\:1|127\.0\.0\.1"'.format(interfaceName) + out = subprocess.Popen(cmdString, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) @@ -415,8 +445,13 @@ def getNetworkIFs(): if len(trimmedLine) > 0: trimmedLines.append(trimmedLine) - print_line('trimmedLines=[{}]'.format(trimmedLines), debug=True) - # + #print_line('interface:[{}] trimmedLines=[{}]'.format(interfaceName, trimmedLines), debug=True) + return trimmedLines + +def loadNetworkIFDetailsFromLines(ifConfigLines): + global rpi_interfaces + global rpi_mac + # # OLDER SYSTEMS # eth0 Link encap:Ethernet HWaddr b8:27:eb:c8:81:f2 # inet addr:192.168.100.41 Bcast:192.168.100.255 Mask:255.255.255.0 @@ -433,10 +468,10 @@ def getNetworkIFs(): haveIF = False imterfc = '' rpi_mac = '' - for currLine in trimmedLines: + for currLine in ifConfigLines: lineParts = currLine.split() - print_line('- currLine=[{}]'.format(currLine), debug=True) - print_line('- lineParts=[{}]'.format(lineParts), debug=True) + #print_line('- currLine=[{}]'.format(currLine), debug=True) + #print_line('- lineParts=[{}]'.format(lineParts), debug=True) if len(lineParts) > 0: # skip interfaces generated by Home Assistant on RPi if 'docker' in currLine or 'veth' in currLine or 'hassio' in currLine: @@ -446,14 +481,14 @@ def getNetworkIFs(): if 'flags' in currLine: # NEWER ONLY haveIF = True imterfc = lineParts[0].replace(':', '') - print_line('newIF=[{}]'.format(imterfc), debug=True) + #print_line('newIF=[{}]'.format(imterfc), debug=True) elif 'Link' in currLine: # OLDER ONLY haveIF = True imterfc = lineParts[0].replace(':', '') newTuple = (imterfc, 'mac', lineParts[4]) if rpi_mac == '': rpi_mac = lineParts[4] - print_line('newIF=[{}]'.format(imterfc), debug=True) + print_line('rpi_mac=[{}]'.format(rpi_mac), debug=True) tmpInterfaces.append(newTuple) print_line('newTuple=[{}]'.format(newTuple), debug=True) elif haveIF == True: @@ -467,6 +502,7 @@ def getNetworkIFs(): tmpInterfaces.append(newTuple) if rpi_mac == '': rpi_mac = lineParts[1] + print_line('rpi_mac=[{}]'.format(rpi_mac), debug=True) print_line('newTuple=[{}]'.format(newTuple), debug=True) haveIF = False @@ -474,6 +510,28 @@ def getNetworkIFs(): print_line('rpi_interfaces=[{}]'.format(rpi_interfaces), debug=True) print_line('rpi_mac=[{}]'.format(rpi_mac), debug=True) +def getNetworkIFs(): + ip_cmd = getIPCmd() + if ip_cmd != '': + getNetworkIFsUsingIP(ip_cmd) + else: + out = subprocess.Popen('/sbin/ifconfig | /bin/egrep "Link|flags|inet |ether " | /bin/egrep -v -i "lo:|loopback|inet6|\:\:1|127\.0\.0\.1"', + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + stdout,_ = out.communicate() + lines = stdout.decode('utf-8').split("\n") + trimmedLines = [] + for currLine in lines: + trimmedLine = currLine.lstrip().rstrip() + if len(trimmedLine) > 0: + trimmedLines.append(trimmedLine) + + print_line('trimmedLines=[{}]'.format(trimmedLines), debug=True) + + loadNetworkIFDetailsFromLines(trimmedLines) + + def getFileSystemDrives(): global rpi_filesystem_space_raw global rpi_filesystem_space @@ -580,6 +638,18 @@ def getVcGenCmd(): desiredCommand = cmd_locn2 return desiredCommand +def getIPCmd(): + cmd_locn1 = '/bin/ip' + cmd_locn2 = '/sbin/ip' + desiredCommand = '' + if os.path.exists(cmd_locn1) == True: + desiredCommand = cmd_locn1 + elif os.path.exists(cmd_locn2) == True: + desiredCommand = cmd_locn2 + if desiredCommand != '': + print_line('Found IP(8)=[{}]'.format(desiredCommand), debug=True) + return desiredCommand + def getSystemTemperature(): global rpi_system_temp global rpi_gpu_temp diff --git a/Release b/Release index 5069169..f33b77a 100644 --- a/Release +++ b/Release @@ -1,2 +1,2 @@ -v1.5.0 Latest -v1.4.4 Stable +v1.5.1 Latest +v1.5.0 Stable