mqtt prep 5

This commit is contained in:
Jokob-sk
2022-11-26 20:24:24 +11:00
parent b86c9b360e
commit 9c81ce9612

View File

@@ -306,7 +306,12 @@ def get_internet_IP ():
# BUGFIX #46 - curl http://ipv4.icanhazip.com repeatedly is very slow # BUGFIX #46 - curl http://ipv4.icanhazip.com repeatedly is very slow
# Using 'dig' # Using 'dig'
dig_args = ['dig', '+short', '-4', 'myip.opendns.com', '@resolver1.opendns.com'] dig_args = ['dig', '+short', '-4', 'myip.opendns.com', '@resolver1.opendns.com']
try:
cmd_output = subprocess.check_output (dig_args, universal_newlines=True) cmd_output = subprocess.check_output (dig_args, universal_newlines=True)
except subprocess.CalledProcessError as e:
print(e.output)
cmd_output = '' # no internet
## BUGFIX #12 - Query IPv4 address (not IPv6) ## BUGFIX #12 - Query IPv4 address (not IPv6)
## Using 'curl' instead of 'dig' ## Using 'curl' instead of 'dig'
@@ -325,7 +330,14 @@ def get_dynamic_DNS_IP ():
# Using default DNS server # Using default DNS server
dig_args = ['dig', '+short', DDNS_DOMAIN] dig_args = ['dig', '+short', DDNS_DOMAIN]
try:
# try runnning a subprocess
dig_output = subprocess.check_output (dig_args, universal_newlines=True) dig_output = subprocess.check_output (dig_args, universal_newlines=True)
except subprocess.CalledProcessError as e:
# An error occured, handle it
print(e.output)
dig_output = '' # probably no internet
# Check result is an IP # Check result is an IP
IP = check_IP_format (dig_output) IP = check_IP_format (dig_output)
@@ -333,6 +345,8 @@ def get_dynamic_DNS_IP ():
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def set_dynamic_DNS_IP (): def set_dynamic_DNS_IP ():
try:
# try runnning a subprocess
# Update Dynamic IP # Update Dynamic IP
curl_output = subprocess.check_output (['curl', '-s', curl_output = subprocess.check_output (['curl', '-s',
DDNS_UPDATE_URL + DDNS_UPDATE_URL +
@@ -340,6 +354,11 @@ def set_dynamic_DNS_IP ():
'&password=' + DDNS_PASSWORD + '&password=' + DDNS_PASSWORD +
'&hostname=' + DDNS_DOMAIN], '&hostname=' + DDNS_DOMAIN],
universal_newlines=True) universal_newlines=True)
except subprocess.CalledProcessError as e:
# An error occured, handle it
print(e.output)
curl_output = ""
return curl_output return curl_output
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@@ -424,7 +443,15 @@ def update_devices_MAC_vendors (pArg = ''):
# Update vendors DB (iab oui) # Update vendors DB (iab oui)
print ('\nUpdating vendors DB (iab & oui)...') print ('\nUpdating vendors DB (iab & oui)...')
update_args = ['sh', PIALERT_BACK_PATH + '/update_vendors.sh', pArg] update_args = ['sh', PIALERT_BACK_PATH + '/update_vendors.sh', pArg]
try:
# try runnning a subprocess
update_output = subprocess.check_output (update_args) update_output = subprocess.check_output (update_args)
except subprocess.CalledProcessError as e:
# An error occured, handle it
print(e.output)
update_output = ""
# DEBUG # DEBUG
# update_args = ['./vendors_db_update.sh'] # update_args = ['./vendors_db_update.sh']
# subprocess.call (update_args, shell=True) # subprocess.call (update_args, shell=True)
@@ -482,7 +509,14 @@ def query_MAC_vendor (pMAC):
# Search vendor in HW Vendors DB # Search vendor in HW Vendors DB
mac = mac[0:6] mac = mac[0:6]
grep_args = ['grep', '-i', mac, VENDORS_DB] grep_args = ['grep', '-i', mac, VENDORS_DB]
# Execute command
try:
# try runnning a subprocess
grep_output = subprocess.check_output (grep_args) grep_output = subprocess.check_output (grep_args)
except subprocess.CalledProcessError as e:
# An error occured, handle it
print(e.output)
grep_output = " There was an error, check logs for details"
# Return Vendor # Return Vendor
vendor = grep_output[7:] vendor = grep_output[7:]
@@ -658,7 +692,15 @@ def execute_arpscan_on_interface (SCAN_SUBNETS):
arpscan_args = ['sudo', 'arp-scan', '--ignoredups', '--retry=6'] + subnets arpscan_args = ['sudo', 'arp-scan', '--ignoredups', '--retry=6'] + subnets
# Execute command # Execute command
return subprocess.check_output (arpscan_args, universal_newlines=True) try:
# try runnning a subprocess
result = subprocess.check_output (arpscan_args, universal_newlines=True)
except subprocess.CalledProcessError as e:
# An error occured, handle it
print(e.output)
result = ""
return result
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def copy_pihole_network (): def copy_pihole_network ():
@@ -1175,7 +1217,15 @@ def resolve_device_name (pMAC, pIP):
# Resolve name with DIG # Resolve name with DIG
dig_args = ['dig', '+short', '-x', pIP] dig_args = ['dig', '+short', '-x', pIP]
# Execute command
try:
# try runnning a subprocess
newName = subprocess.check_output (dig_args, universal_newlines=True) newName = subprocess.check_output (dig_args, universal_newlines=True)
except subprocess.CalledProcessError as e:
# An error occured, handle it
print(e.output)
newName = "Error - check logs"
# Check returns # Check returns
newName = newName.strip() newName = newName.strip()
@@ -1759,12 +1809,17 @@ def send_webhook (_json, _html):
curlParams = ["curl","-i","-X", webhookRequestMethod ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), _WEBHOOK_URL] curlParams = ["curl","-i","-X", webhookRequestMethod ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), _WEBHOOK_URL]
# execute CURL call # execute CURL call
try:
# try runnning a subprocess
p = subprocess.Popen(curlParams, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p = subprocess.Popen(curlParams, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
# write stdout and stderr into .log files for debugging if needed # write stdout and stderr into .log files for debugging if needed
logResult (stdout, stderr) logResult (stdout, stderr)
except subprocess.CalledProcessError as e:
# An error occured, handle it
print(e.output)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def send_apprise (html): def send_apprise (html):
@@ -1776,12 +1831,15 @@ def send_apprise (html):
"body": html "body": html
} }
try:
# try runnning a subprocess
p = subprocess.Popen(["curl","-i","-X", "POST" ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), APPRISE_HOST], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) p = subprocess.Popen(["curl","-i","-X", "POST" ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), APPRISE_HOST], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
# write stdout and stderr into .log files for debugging if needed # write stdout and stderr into .log files for debugging if needed
logResult (stdout, stderr) logResult (stdout, stderr)
except subprocess.CalledProcessError as e:
# An error occured, handle it
print(e.output)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
mqtt_connected_to_broker = 0 mqtt_connected_to_broker = 0