Skip to content

Instantly share code, notes, and snippets.

@initialed85
Created February 1, 2018 14:18
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save initialed85/abae063f04c819c867255f804cb0709f to your computer and use it in GitHub Desktop.
Save initialed85/abae063f04c819c867255f804cb0709f to your computer and use it in GitHub Desktop.
Example interaction with Ubiquiti AirOS "API"
# Requirements:
#
# Python 2.7
# Requests package (pip install requests)
import pprint
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
USERNAME = 'some_readonly_username'
PASSWORD = 'some_readonly_password'
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
login_url = 'https://{0}/api/auth'
urls = [
'http://{0}/arp.cgi',
'http://{0}/brmacs.cgi?brmacs=y',
'http://{0}/sroutes.cgi',
'http://{0}/status.cgi',
# the following don't seem to work on AirOS 8
'http://{0}/getcfg.sh',
'http://{0}/getboardinfo.sh',
'http://{0}/sta.cgi',
'http://{0}/ifstats.cgi',
'http://{0}/iflist.cgi',
'http://{0}/log.cgi',
]
ips = [
'1.2.3.4',
'5.6.7.8',
]
responses_by_ip = {}
for ip in ips:
# keep a session
with requests.Session() as s:
# conduct the login
r = s.post(
url=login_url.format(ip),
data={
'username': USERNAME,
'password': PASSWORD,
},
verify=False,
timeout=5,
)
# skip this IP if the login fails
if r.status_code > 201:
continue
responses = {}
# scrape the URLs
for url in urls:
url = url.format(ip)
r = s.get(
url=url,
verify=False
)
responses.update({url: r.json() if r.status_code == 200 else r.status_code})
responses_by_ip.update({ip: responses})
pprint.pprint(responses_by_ip)
@rifatdinc
Copy link

Thank you very much for this code. It saved my life.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment