Skip to content

Instantly share code, notes, and snippets.

@furlongm
Last active July 10, 2024 09:52
Show Gist options
  • Save furlongm/a194697b7aa171411b7fffe2a64b06cc to your computer and use it in GitHub Desktop.
Save furlongm/a194697b7aa171411b7fffe2a64b06cc to your computer and use it in GitHub Desktop.
patchman-api-excel-report.py
#!/usr/bin/env python3
import json
import requests
import xlsxwriter
from datetime import datetime
from pprint import pprint
protocol = 'http'
host = '34.122.254.38'
path = '/api/'
username = 'admin'
password = 'patchman'
def setup_api_url(protocol, host, path):
return f'{protocol}://{host}{path}'
def setup_api_auth(username, password):
return (username, password)
def request_api_url(api_url, api_auth):
return requests.get(url=api_url, auth=api_auth)
def get_all_hosts(endpoints):
r = requests.get(f'{endpoints["host"]}')
if r.status_code == 200:
return r.json()['results']
def get_host_by_id(endpoints, host_id):
r = requests.get(f'{endpoints["host"]}{host_id}/')
if r.status_code == 200:
host = r.json()
return host
def get_object_by_url(api_url, api_auth):
r = request_api_url(api_url, api_auth)
if r.status_code == 200:
return r.json()
def write_excel_host_report(hosts):
now = datetime.now().isoformat()
name = f'patchman-host-report-{now}.xlsx'
workbook = xlsxwriter.Workbook(f'{name}')
worksheet = workbook.add_worksheet('hosts')
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'ID', bold)
worksheet.write('B1', 'Hostname', bold)
worksheet.write('C1', 'OS', bold)
worksheet.write('D1', 'Updates', bold)
max_hostname = max_os_name = 0
for row, host in enumerate(hosts, 1):
hostname = host['hostname']
if len(hostname) > max_hostname:
max_hostname = len(hostname)
os_url = host['os']
os = get_object_by_url(os_url, api_auth)
os_name = os['name']
if len(os_name) > max_os_name:
max_os_name = len(os_name)
worksheet.write(row, 0, host['id'])
worksheet.write(row, 1, hostname)
worksheet.write(row, 2, os_name)
worksheet.write(row, 3, len(host['updates']))
worksheet.set_column(1, 1, max_hostname)
worksheet.set_column(2, 2, max_os_name)
workbook.close()
# set up api
api_auth = setup_api_auth(username, password)
endpoints_url = setup_api_url(protocol, host, path)
endpoints = get_object_by_url(endpoints_url, api_auth)
# create excel report
hosts = get_all_hosts(endpoints)
write_excel_host_report(hosts)
@tabbitsp
Copy link

tabbitsp commented Jul 10, 2024

This is quite helpful, but if you have a lot of machines, thus having more than one page, the results will be incomplete.
A better version paging through should be this one:

def get_all_hosts(endpoints):
    r = requests.get(f'{endpoints["host"]}')
    if r.status_code == 200:
        data = r.json()['results']
        while r.json()['next'] is not None:
            r = requests.get(r.json()['next'])
            if r.status_code == 200:
                data = data + r.json()['results']
        return data

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