Last active
August 28, 2020 09:05
-
-
Save sergiomario/4b1999554365871413609bcc53e211db to your computer and use it in GitHub Desktop.
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
import json | |
import requests | |
BASE_URL = 'https://192.168.6.35/cgi-bin' | |
API = { | |
'apply': BASE_URL+'/api/v3/system/apply', | |
'login': BASE_URL+'/api/v3/system/login', | |
'service_led': BASE_URL+'/api/v3/service/leds', | |
'service_ssh': BASE_URL+'/api/v3/service/ssh', | |
} | |
headers = {'content-type': 'application/json'} | |
def auth(username, password): | |
data = {'data': {'username': username, 'password': password}} | |
response = requests.post(API['login'], data=json.dumps(data), headers=headers, verify=False) | |
if response.status_code == 200: | |
token = json.loads(response.content.decode('utf-8'))['data']['Token'] | |
headers['Authorization'] = 'Bauer ' + token | |
return True | |
return False | |
def apply(): | |
response = requests.post(API['apply'], headers=headers, verify=False) | |
result = json.loads(response.content.decode('utf-8')) | |
if result['data']['sucess']: | |
return result['data']['config_hash'] | |
return False | |
def change_led_color(color): | |
response = requests.get(API['service_led'], headers=headers, verify=False) | |
led_options = json.loads(response.content.decode('utf-8')) | |
if color == 'off': | |
led_options['data']['action']['value'] = 'off' | |
else: | |
led_options['data']['color']['value'] = color | |
requests.put(API['service_led'], data=json.dumps(led_options), headers=headers, verify=False) | |
def ssh_config(enabled=True, port=22, wan_access=True): | |
data = {'data': {'enabled': enabled, 'port': port, 'wan_access': wan_access}} | |
requests.put(API['service_ssh'], data=json.dumps(data), headers=headers, verify=False) | |
if auth('admin', 'admin'): | |
change_led_color('white') | |
ssh_config() | |
apply() | |
else: | |
print('Username or password is invalid') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment