From c21497c61e61016e7264109ce38cb79cfa3e0ccc Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Mon, 5 Aug 2024 21:55:11 +1000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8CUNIFI=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/plugins/plugin_helper.py | 18 ++++++++++++++++++ front/plugins/unifi_import/script.py | 12 +++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/front/plugins/plugin_helper.py b/front/plugins/plugin_helper.py index d1a6ae11..1d8b4d9c 100755 --- a/front/plugins/plugin_helper.py +++ b/front/plugins/plugin_helper.py @@ -59,6 +59,24 @@ def rmBadChars(input): return input +# ------------------------------------------------------------------- +# check if this is a router IP +def is_typical_router_ip(ip_address): + # List of common default gateway IP addresses + common_router_ips = [ + "192.168.0.1", "192.168.1.1", "192.168.1.254", "192.168.0.254", + "10.0.0.1", "10.1.1.1", "192.168.2.1", "192.168.10.1", "192.168.11.1", + "192.168.100.1", "192.168.101.1", "192.168.123.254", "192.168.223.1", + "192.168.31.1", "192.168.8.1", "192.168.254.254", "192.168.50.1", + "192.168.3.1", "192.168.4.1", "192.168.5.1", "192.168.9.1", + "192.168.15.1", "192.168.16.1", "192.168.20.1", "192.168.30.1", + "192.168.42.1", "192.168.62.1", "192.168.178.1", "192.168.1.1", + "192.168.1.254", "192.168.0.1", "192.168.0.10", "192.168.0.100", + "192.168.0.254" + ] + + return ip_address in common_router_ips + # ------------------------------------------------------------------- # Check if a valid MAC address def is_mac(input): diff --git a/front/plugins/unifi_import/script.py b/front/plugins/unifi_import/script.py index 06792fde..291ed097 100755 --- a/front/plugins/unifi_import/script.py +++ b/front/plugins/unifi_import/script.py @@ -19,7 +19,7 @@ from pyunifi.controller import Controller INSTALL_PATH="/app" sys.path.extend([f"{INSTALL_PATH}/front/plugins", f"{INSTALL_PATH}/server"]) -from plugin_helper import Plugin_Object, Plugin_Objects, rmBadChars +from plugin_helper import Plugin_Object, Plugin_Objects, rmBadChars, is_typical_router_ip from logger import mylog from helper import timeNowTZ, get_setting_value import conf @@ -158,11 +158,17 @@ def collect_details(device_type, devices, online_macs, processed_macs, plugin_ob for device in devices: mylog('verbose', [f'{json.dumps(device)}']) + # try extracting variables from teh json name = get_name(get_unifi_val(device, 'name'), get_unifi_val(device, 'hostname')) - ipTmp = get_ip(get_unifi_val(device, 'last_ip'), get_unifi_val(device, 'fixed_ip'), get_unifi_val(device, 'ip')) + ipTmp = get_ip(get_unifi_val(device, 'lan_ip'), get_unifi_val(device, 'last_ip'), get_unifi_val(device, 'fixed_ip'), get_unifi_val(device, 'ip')) macTmp = device['mac'] status = 1 if macTmp in online_macs else device.get('state', 0) deviceType = device_type.get(device.get('type'), '') + parentMac = get_parent_mac(get_unifi_val(device, 'uplink_mac'), get_unifi_val(device, 'ap_mac'), get_unifi_val(device, 'sw_mac')) + + # override parent MAC if this is a router + if parentMac == 'null' and is_typical_router_ip(ipTmp): + parentMac = 'Internet' # Add object only if not processed if macTmp not in processed_macs: @@ -175,7 +181,7 @@ def collect_details(device_type, devices, online_macs, processed_macs, plugin_ob watched4=status, extra=get_unifi_val(device, 'connection_network_name', ''), foreignKey="", - helpVal1=get_parent_mac(get_unifi_val(device, 'uplink_mac'), get_unifi_val(device, 'ap_mac'), get_unifi_val(device, 'sw_mac')), + helpVal1=parentMac, helpVal2=get_port(get_unifi_val(device, 'sw_port'), get_unifi_val(device, 'uplink_remote_port')), helpVal3=device_label, helpVal4="",