Last active
May 17, 2023 14:20
-
-
Save tarrenj/00bc5294acfc51624a24baf940105399 to your computer and use it in GitHub Desktop.
Change the default GW of a zcube encoder as the network changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
A garbage hacky script to automatically reconfigure the default gateway of the zcube video encoder | |
based on which network route is available. | |
Preferance is given to one link! | |
Must be run as root! - This might not be true anymore... /shrug | |
""" | |
import time | |
import os | |
import datetime | |
import requests | |
import subprocess | |
import json | |
encoder = '' | |
los_gw = '' | |
satcom_gw = '' # Hub Router | |
gateways = [los_gw, satcom_gw] | |
preferred_gw = satcom_gw | |
requests_timeout = 5 | |
cycle_time = 15 # var so we can adjust if the zcube reboots slower | |
def ping(target): | |
""" | |
Returns a boolean if the target responds to a ping | |
""" | |
fnull = open(os.devnull, 'w') | |
result = subprocess.run(['ping', '-c', '4', target], stdout=fnull, | |
stderr=fnull).returncode | |
fnull.close() | |
return result == 0 | |
def reconfigure(new_gw): | |
""" | |
Reconfigure the ZCube encoder to use the new default gateway | |
""" | |
# Set the headers once | |
headers = { | |
'Origin': 'http://' + encoder, | |
'Accept': 'application/json, text/javascript, */*; q=0.01', | |
'X-Requested-With': 'XMLHttpRequest', | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0', | |
'Connection': 'close', | |
'Referer': 'http://' + encoder + '/', | |
'Accept-Language': 'en-US,en;q=0.5', | |
'Accept-Encoding': 'gzip, deflate', | |
'DNT': '1', | |
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
} | |
session = requests.Session() | |
try: | |
# Send the change | |
params_post = { | |
'local_ip': encoder, | |
'use_dhcp': '0', | |
'local_dnsip2': '8.8.4.4', | |
'do_autostart': '1', | |
'local_dnsip': '8.8.8.8', | |
'local_netmask': '255.255.255.0', | |
'enc_adv_setting': 'on', | |
'action': 'SetIp', | |
'ipmtu': '1500', | |
'default_gw': new_gw, | |
} | |
response = session.post('http://' + encoder + '/cgi-bin/control.cgi', | |
data=params_post, headers=headers, timeout=requests_timeout) | |
if response.status_code != 200: | |
print(datetime.datetime.now(), "- Reconfiguring didn't work for some reason! Status Code:", | |
response.status_code) | |
# Save the change | |
params_post = {'action': 'SaveUser', 'enc_current_preset': 'encoder'} | |
response = session.post('http://' + encoder + '/cgi-bin/control.cgi', | |
data=params_post, headers=headers, timeout=requests_timeout) | |
if response.status_code != 200: | |
print(datetime.datetime.now(), "- Saving didn't work for some reason! Status Code:", | |
response.status_code) | |
# Reboot the device | |
params_post = {'action': 'RestartBoard'} | |
response = session.post('http://' + encoder + '/cgi-bin/control.cgi', | |
data=params_post, headers=headers, timeout=requests_timeout) | |
if response.status_code != 200: | |
print(datetime.datetime.now(), response.status_code, | |
"- Unable to restart the encoder for some reason! Status Code:") | |
except requests.exceptions.RequestException as e: | |
print(datetime.datetime.now(), "- Reconfiguring did not work for some reason!", e) | |
time.sleep(cycle_time) | |
if __name__ == '__main__': | |
while True: | |
# Figure out what the current default_gw is | |
current_gw = '' | |
while not current_gw: | |
try: | |
system_request = requests.get('http://' + encoder | |
+ '/cgi-bin/control.cgi?ctrl=sys&chn=', timeout=requests_timeout) | |
json_values = \ | |
json.loads(system_request.content.decode('utf-8')) | |
current_gw: str = json_values['default_gw'] | |
other_gw: str = [x for x in gateways if x != current_gw][0] | |
except requests.exceptions.RequestException as e: | |
# The encoder is likely still booting up | |
time.sleep(5) | |
pass | |
# Ensure the current GW is available, check and swap to preferred GW if needed. | |
while True: | |
if ping(current_gw): | |
if current_gw == preferred_gw: | |
# Already at preferred GW, and preferred GW is up | |
time.sleep(cycle_time) | |
else: | |
if ping(preferred_gw): | |
print(datetime.datetime.now(), | |
'- Preferred Link Reestablished - Reconfiguring ZCube Encoder...') | |
reconfigure(preferred_gw) | |
time.sleep(cycle_time) | |
break | |
else: | |
print(datetime.datetime.now(), | |
'- {} Link Lost!'.format(current_gw)) | |
# See if the other route is up | |
if ping(other_gw): | |
print(datetime.datetime.now(), | |
'- {} Link Detected - Reconfiguring ZCube Encoder...'.format(other_gw)) | |
reconfigure(other_gw) | |
time.sleep(cycle_time) | |
break | |
else: | |
print(datetime.datetime.now(), | |
'- NO active link detected! trying again after a short while...') | |
time.sleep(cycle_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment