ddns plugin 0.1 + internet_ip 0.4
This commit is contained in:
@@ -43,31 +43,6 @@
|
||||
"type": "sql",
|
||||
"value": "SELECT dev_LastIP FROM Devices WHERE dev_MAC = 'Internet' "
|
||||
},
|
||||
{
|
||||
"name": "DDNS_ACTIVE",
|
||||
"type": "setting",
|
||||
"value": "DDNS_ACTIVE"
|
||||
},
|
||||
{
|
||||
"name": "DDNS_UPDATE_URL",
|
||||
"type": "setting",
|
||||
"value": "DDNS_UPDATE_URL"
|
||||
},
|
||||
{
|
||||
"name": "DDNS_USER",
|
||||
"type": "setting",
|
||||
"value": "DDNS_USER"
|
||||
},
|
||||
{
|
||||
"name": "DDNS_PASSWORD",
|
||||
"type": "setting",
|
||||
"value": "DDNS_PASSWORD"
|
||||
},
|
||||
{
|
||||
"name": "DDNS_DOMAIN",
|
||||
"type": "setting",
|
||||
"value": "DDNS_DOMAIN"
|
||||
},
|
||||
{
|
||||
"name": "DIG_GET_IP_ARG",
|
||||
"type": "setting",
|
||||
@@ -108,14 +83,14 @@
|
||||
"description": [
|
||||
{
|
||||
"language_code": "en_us",
|
||||
"string": "When the cleanup should be performed. An hourly or daily <code>SCHEDULE</code> is a good option."
|
||||
"string": "When the plugin should run. An hourly or daily <code>SCHEDULE</code> is a good option."
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"function": "CMD",
|
||||
"type": "readonly",
|
||||
"default_value": "python3 /home/pi/pialert/front/plugins/internet_ip/script.py prev_ip={prev_ip} DDNS_ACTIVE={DDNS_ACTIVE} DDNS_UPDATE_URL={DDNS_UPDATE_URL} DDNS_USER={DDNS_USER} DDNS_PASSWORD={DDNS_PASSWORD} DDNS_DOMAIN={DDNS_DOMAIN} DIG_GET_IP_ARG={DIG_GET_IP_ARG}",
|
||||
"default_value": "python3 /home/pi/pialert/front/plugins/internet_ip/script.py prev_ip={prev_ip} DIG_GET_IP_ARG={DIG_GET_IP_ARG}",
|
||||
"options": [],
|
||||
"localized": [
|
||||
"name",
|
||||
@@ -255,7 +230,7 @@
|
||||
"description": [
|
||||
{
|
||||
"language_code": "en_us",
|
||||
"string": "Send a notification if selected values change. Use <code>CTRL + Click</code> to select/deselect. <ul> <li><code>Watched_Value1</code> is Internet IP/li><li><code>Watched_Value2</code> is Previous IP (not recommended)</li><li><code>Watched_Value3</code> unused </li><li><code>Watched_Value4</code> unused </li></ul>"
|
||||
"string": "Send a notification if selected values change. Use <code>CTRL + Click</code> to select/deselect. <ul> <li><code>Watched_Value1</code> is Previous IP (not recommended)</li><li><code>Watched_Value2</code> unused</li><li><code>Watched_Value3</code> unused </li><li><code>Watched_Value4</code> unused </li></ul>"
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -307,7 +282,7 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"column": "Watched_Value1",
|
||||
"column": "Object_SecondaryID",
|
||||
"mapped_to_column": "cur_IP",
|
||||
"css_classes": "col-sm-2",
|
||||
"show": true,
|
||||
@@ -329,7 +304,7 @@
|
||||
]
|
||||
},
|
||||
{
|
||||
"column": "Watched_Value2",
|
||||
"column": "Watched_Value1",
|
||||
"css_classes": "col-sm-2",
|
||||
"show": true,
|
||||
"type": "label",
|
||||
|
||||
@@ -35,63 +35,30 @@ def main():
|
||||
parser = argparse.ArgumentParser(description='Check internet connectivity and IP')
|
||||
|
||||
parser.add_argument('prev_ip', action="store", help="Previous IP address to compare against the current IP")
|
||||
parser.add_argument('DDNS_ACTIVE', action="store", help="Indicates if Dynamic DNS (DDNS) is active (True/False)")
|
||||
parser.add_argument('DDNS_UPDATE_URL', action="store", help="URL for updating Dynamic DNS (DDNS)")
|
||||
parser.add_argument('DDNS_USER', action="store", help="Username for Dynamic DNS (DDNS) authentication")
|
||||
parser.add_argument('DDNS_PASSWORD', action="store", help="Password for Dynamic DNS (DDNS) authentication")
|
||||
parser.add_argument('DDNS_DOMAIN', action="store", help="Dynamic DNS (DDNS) domain name")
|
||||
parser.add_argument('DIG_GET_IP_ARG', action="store", help="Arguments for the 'dig' command to retrieve the IP address")
|
||||
|
||||
values = parser.parse_args()
|
||||
|
||||
PREV_IP = values.prev_ip.split('=')[1]
|
||||
DDNS_ACTIVE = values.DDNS_ACTIVE.split('=')[1]
|
||||
DDNS_UPDATE_URL = values.DDNS_UPDATE_URL.split('=')[1]
|
||||
DDNS_USER = values.DDNS_USER.split('=')[1]
|
||||
DDNS_PASSWORD = values.DDNS_PASSWORD.split('=')[1]
|
||||
DDNS_DOMAIN = values.DDNS_DOMAIN.split('=')[1]
|
||||
DIG_GET_IP_ARG = values.DIG_GET_IP_ARG.split('=')[1]
|
||||
PREV_IP = values.prev_ip.split('=')[1]
|
||||
DIG_GET_IP_ARG = values.DIG_GET_IP_ARG.split('=b')[1] # byte64 encoded
|
||||
|
||||
mylog('verbose', ['[INTRNT] DIG_GET_IP_ARG: ', DIG_GET_IP_ARG])
|
||||
mylog('verbose', ['[INTRNT] DIG_GET_IP_ARG: ', DIG_GET_IP_ARG])
|
||||
|
||||
# Decode the base64-encoded value to get the actual value in ASCII format.
|
||||
DIG_GET_IP_ARG = base64.b64decode(DIG_GET_IP_ARG.split('b')[1]).decode('ascii')
|
||||
DIG_GET_IP_ARG = base64.b64decode(DIG_GET_IP_ARG).decode('ascii')
|
||||
|
||||
mylog('verbose', [f'[INTRNT] DIG_GET_IP_ARG resolved: {DIG_GET_IP_ARG} '])
|
||||
|
||||
# if internet_IP != "" :
|
||||
# sql.execute (f"""INSERT INTO CurrentScan (cur_MAC, cur_IP, cur_Vendor, cur_ScanMethod)
|
||||
# VALUES ( 'Internet', '{internet_IP}', Null, 'queryDNS') """)
|
||||
|
||||
# # Save event
|
||||
# cursor.execute ("""INSERT INTO Events (eve_MAC, eve_IP, eve_DateTime,
|
||||
# eve_EventType, eve_AdditionalInfo,
|
||||
# eve_PendingAlertEmail)
|
||||
# VALUES ('Internet', ?, ?, 'Internet IP Changed',
|
||||
# 'Previous Internet IP: '|| ?, 1) """,
|
||||
# (pNewIP, timeNowTZ(), prevIp) )
|
||||
|
||||
|
||||
# # Save new IP
|
||||
# cursor.execute ("""UPDATE Devices SET dev_LastIP = ?
|
||||
# WHERE dev_MAC = 'Internet' """,
|
||||
# (pNewIP,) )
|
||||
|
||||
|
||||
# Object_PrimaryID - cur_MAC
|
||||
# Watched_Value1 - cur_IP
|
||||
# Watched_Value2 - Extra / prev IP
|
||||
|
||||
|
||||
new_internet_IP = check_internet_IP( DDNS_ACTIVE, DDNS_UPDATE_URL, DDNS_USER, DDNS_PASSWORD, DDNS_DOMAIN, PREV_IP, DIG_GET_IP_ARG)
|
||||
# perform the new IP lookup
|
||||
new_internet_IP = check_internet_IP( PREV_IP, DIG_GET_IP_ARG)
|
||||
|
||||
plugin_objects = Plugin_Objects(RESULT_FILE)
|
||||
|
||||
plugin_objects.add_object(
|
||||
primaryId = 'Internet', # MAC (Device Name)
|
||||
secondaryId = '',
|
||||
watched1 = new_internet_IP, # IP Address
|
||||
watched2 = f'Previous IP: {PREV_IP}',
|
||||
secondaryId = new_internet_IP, # IP Address
|
||||
watched1 = f'Previous IP: {PREV_IP}',
|
||||
watched2 = '',
|
||||
watched3 = '',
|
||||
watched4 = '',
|
||||
extra = f'Previous IP: {PREV_IP}',
|
||||
@@ -102,104 +69,36 @@ def main():
|
||||
mylog('verbose', ['[INTRNT] Finished '])
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
|
||||
#===============================================================================
|
||||
# INTERNET IP CHANGE
|
||||
#===============================================================================
|
||||
def check_internet_IP ( DDNS_ACTIVE, DDNS_UPDATE_URL, DDNS_USER, DDNS_PASSWORD, DDNS_DOMAIN, PREV_IP, DIG_GET_IP_ARG ):
|
||||
def check_internet_IP ( PREV_IP, DIG_GET_IP_ARG ):
|
||||
|
||||
# Get Internet IP
|
||||
mylog('verbose', ['[INTRNT] - Retrieving Internet IP'])
|
||||
internet_IP = get_internet_IP(DIG_GET_IP_ARG)
|
||||
|
||||
# Check result = IP
|
||||
if internet_IP == "" :
|
||||
mylog('none', ['[INTRNT] Error retrieving Internet IP'])
|
||||
mylog('verbose', [f'[INTRNT] Current internet_IP : {internet_IP}'])
|
||||
|
||||
# Get previous stored IP
|
||||
# Check previously stored IP
|
||||
previous_IP = '0.0.0.0'
|
||||
|
||||
if PREV_IP is not None and len(PREV_IP) > 0 :
|
||||
previous_IP = PREV_IP
|
||||
|
||||
mylog('verbose', ['[INTRNT] ', previous_IP])
|
||||
mylog('verbose', [f'[INTRNT] previous_IP : {previous_IP}'])
|
||||
|
||||
# logging
|
||||
append_line_to_file (logPath + '/IP_changes.log', '['+str(timeNowTZ()) +']\t'+ internet_IP +'\n')
|
||||
|
||||
# Get Dynamic DNS IP
|
||||
if DDNS_ACTIVE :
|
||||
mylog('verbose', ['[DDNS] Retrieving Dynamic DNS IP'])
|
||||
dns_IP = get_dynamic_DNS_IP(DDNS_DOMAIN)
|
||||
|
||||
# Check Dynamic DNS IP
|
||||
if dns_IP == "" or dns_IP == "0.0.0.0" :
|
||||
mylog('none', ['[DDNS] Error retrieving Dynamic DNS IP'])
|
||||
mylog('none', ['[DDNS] ', dns_IP])
|
||||
|
||||
# Check DNS Change
|
||||
if dns_IP != internet_IP :
|
||||
mylog('none', ['[DDNS] Updating Dynamic DNS IP'])
|
||||
message = set_dynamic_DNS_IP (DDNS_UPDATE_URL, DDNS_USER, DDNS_PASSWORD, DDNS_DOMAIN)
|
||||
mylog('none', ['[DDNS] ', message])
|
||||
else :
|
||||
mylog('verbose', ['[DDNS] No changes to perform'])
|
||||
else :
|
||||
mylog('verbose', ['[DDNS] Skipping Dynamic DNS update'])
|
||||
append_line_to_file (logPath + '/IP_changes.log', '['+str(timeNowTZ()) +']\t'+ internet_IP +'\n')
|
||||
|
||||
return internet_IP
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
def get_dynamic_DNS_IP (DDNS_DOMAIN):
|
||||
# Using OpenDNS server
|
||||
# dig_args = ['dig', '+short', DDNS_DOMAIN, '@resolver1.opendns.com']
|
||||
|
||||
# Using default DNS server
|
||||
dig_args = ['dig', '+short', DDNS_DOMAIN]
|
||||
|
||||
try:
|
||||
# try runnning a subprocess
|
||||
dig_output = subprocess.check_output (dig_args, universal_newlines=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
# An error occured, handle it
|
||||
mylog('none', ['[DDNS] ERROR - ', e.output])
|
||||
dig_output = '' # probably no internet
|
||||
|
||||
# Check result is an IP
|
||||
IP = check_IP_format (dig_output)
|
||||
|
||||
# Handle invalid response
|
||||
if IP == '':
|
||||
IP = '0.0.0.0'
|
||||
|
||||
return IP
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
def set_dynamic_DNS_IP (DDNS_UPDATE_URL, DDNS_USER, DDNS_PASSWORD, DDNS_DOMAIN):
|
||||
try:
|
||||
# try runnning a subprocess
|
||||
# Update Dynamic IP
|
||||
curl_output = subprocess.check_output (['curl',
|
||||
'-s',
|
||||
DDNS_UPDATE_URL +
|
||||
'username=' + DDNS_USER +
|
||||
'&password=' + DDNS_PASSWORD +
|
||||
'&hostname=' + DDNS_DOMAIN],
|
||||
universal_newlines=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
# An error occured, handle it
|
||||
mylog('none', ['[DDNS] ERROR - ',e.output])
|
||||
curl_output = ""
|
||||
|
||||
return curl_output
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
def get_internet_IP (DIG_GET_IP_ARG):
|
||||
# BUGFIX #46 - curl http://ipv4.icanhazip.com repeatedly is very slow
|
||||
|
||||
# Using 'dig'
|
||||
dig_args = ['dig', '+short'] + DIG_GET_IP_ARG.strip().split()
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user