Skip to content

Instantly share code, notes, and snippets.

@rus-kilian
Created January 12, 2021 21:35
Show Gist options
  • Save rus-kilian/5b3e24786988fa17ed28ce1cf07d6d2c to your computer and use it in GitHub Desktop.
Save rus-kilian/5b3e24786988fa17ed28ce1cf07d6d2c to your computer and use it in GitHub Desktop.
Compare Aruba https and SSH API calls
#!/usr/bin/env python3
import os
import sys
import time
import yaml
import pprint
import requests
import urllib3
from netmiko import ConnectHandler
if len(sys.argv) < 2:
print('Usage %s <md> <command>' % os.path.basename(__file__))
sys.exit(1)
config={}
if os.path.isfile(os.environ['HOME'] + '/.config.yaml'):
with open(os.environ['HOME'] + "/.config.yaml", 'r') as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
exit(1)
else:
print('No config.yaml')
exit(1)
pp = pprint.PrettyPrinter(indent=4)
session_cookies = {}
urllib3.disable_warnings()
http_session = requests.Session()
http_session.verify = False
def target_login(target):
global config
global session_cookies
login_url = "https://" + target + ":4343/v1/api/login"
# Initiate login with authentication, and persistently store cookies
try:
login_response = http_session.get(login_url, params={'username': config['login'], 'password': config['password']}, timeout=(1,3))
except Timeout:
return False
else:
# Login to store UIDARUBA
if login_response:
http_session_arubauid = login_response.json()['_global_result']['UIDARUBA']
session_cookies[target] = http_session_arubauid
return http_session_arubauid
else:
# FIXME: add retry
return False
def showcli(target,command,retries=3):
global session_cookies
if target not in session_cookies:
if not target_login(target):
return
show_command_url = "https://" + target + ":4343/v1/configuration/showcommand"
for i in range(0,retries):
try:
print('Fetching "%s" from "%s" via https API' % (command,target))
show_command_response = http_session.get(show_command_url, params={"json": "1", "command": command, 'UIDARUBA': session_cookies[target]}, timeout=(3,120), headers={'Connection':'close'})
except Timeout:
http_session.close()
else:
http_session.close()
if show_command_response.status_code == 200:
if show_command_response.text == '':
print('! ! ! ! ! ! ! ! ! ! Empty response received ! ! ! ! ! ! ! ! ! !')
return
else:
try:
json = show_command_response.json()
except:
print('Invalid response received! Not JSON:')
print(show_command_response.text)
return
else:
return json
elif show_command_response.status_code == 401:
print('Unauthenticated on %s. Retrying.' % target)
if target_login(target):
return showcli(target,command)
else:
return
else:
print('! ! ! ! ! ! ! ! ! ! ERROR %d - %s ! ! ! ! ! ! ! ! ! !' % (show_command_response.status_code,show_command_response.text))
return
print('Retries exceeded with Timeout for "%s" on %s' % (command,target))
return
md = sys.argv[1]
command = ' '.join(sys.argv[2:])
print('Running "%s" on %s in https vs. SSH mode' % (command,md))
print('----------------------------------------------------------------------------')
print('Starting https API call:')
start_time = time.time()
print('############################################################################')
output = showcli(md,command)
http_time = time.time() - start_time
if output:
pp.pprint(output)
else:
print('! ! ! ! ! ! ! ! ! ! FAILED ! ! ! ! ! ! ! ! ! !')
print('############################################################################')
print('Time taken: %ds' % http_time)
print('----------------------------------------------------------------------------')
print('Starting SSH call:')
start_time = time.time()
ak_login = {
'device_type': 'aruba_os',
'host': md,
'username': config['login'],
'password': config['password'],
}
net_connect = ConnectHandler(**ak_login)
connect_time = time.time()
connect_delay = connect_time - start_time
print('Connect delay: %ds' % connect_delay)
print('Fetching "%s" from "%s" via SSH' % (command,md))
print('############################################################################')
output = net_connect.send_command(command)
ssh_time = time.time() - connect_time
if output:
output = output.replace('\r', '')
print(output)
else:
print('! ! ! ! ! ! ! ! ! ! FAILED ! ! ! ! ! ! ! ! ! !')
print('############################################################################')
print('Time taken: %ds [command] + %ds [connect] over SSH (vs. %ds over https)' % (ssh_time,connect_delay,http_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment