From a27b3b2e7454b66f077ded9a4bc4da3b9595d0d5 Mon Sep 17 00:00:00 2001 From: Lucas Hocker Date: Tue, 10 Apr 2018 13:08:14 -0500 Subject: [PATCH] Updated to support python3 and added usage info for Mac & Windows --- ExcelExport/README.md | 41 +++++++++++++++++++++++++++++++++++++++++ ExcelExport/dt-excel.py | 16 ++++++++-------- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/ExcelExport/README.md b/ExcelExport/README.md index c9cefe0..b9f1577 100644 --- a/ExcelExport/README.md +++ b/ExcelExport/README.md @@ -11,3 +11,44 @@ This example demonstrates the use of Topology endpoints and small amounts of cod - Somehow show multiple IPs & ports better in the spreadsheet - Incorporate Application & Service data as well - Explore real-world possible uses + +## Getting Started +This example app is very simple and does not have much logic built in. On most OSes you'll already have python, but here are more detailed steps in case you're missing dependencies: +### Most Linux varients (assuming yum packages, sub in your package manager where necessary) +- Run: which python >/dev/null || sudo yum -y install python +- Run: which pip >/dev/null || sudo yum -y install python2-pip +- Run: sudo pip install pycurl +- Run: sudo pip install certifi +- Run: sudo pip install openpyxl +- Edit: dt-excel.py, change variable in SETUP VARIABLES section +- Run: python dt-excel.py + +### MacOS (High Sierra) +- Run: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" +- Run: brew install openssl +- Run: brew install python +- Confirm: "which python3" gives /usr/local/bin/python3 +- Run: sudo pip3 install --upgrade pip +- Run: pip3 install --user --install-option="--with-openssl" --install-option="--openssl-dir=/usr/local/opt/openssl" pycurl +- Run: pip3 install --user openpyxl +- Run: pip3 install --user certifi +- Edit: dt-excel.py, change variable in SETUP VARIABLES section +- Run: python3 dt-excel.py + +### Windows +- Download latest: https://www.python.org/downloads/windows +- Install: be sure to pick options for "add to PATH" and "include PIP" +- Open command prompt +- Run: pip install pycurl +- Run: pip install certifi +- Run: pip install openpyxl +- Edit: dt-excel.py, change variable in SETUP VARIABLES section +- Run: python dt-excel.py + +## Troubleshooting +- Status: 401 +- - Your API token didn't work, please check the token and URL +- Status: 400 +- - Your URL is incorrect +- Dependency or module not found errors +- - Your python install is incorrect or modules are missing, try the pip steps again and check for any errors there diff --git a/ExcelExport/dt-excel.py b/ExcelExport/dt-excel.py index 56d6b44..3be3608 100644 --- a/ExcelExport/dt-excel.py +++ b/ExcelExport/dt-excel.py @@ -1,7 +1,8 @@ import pycurl import json import csv -from StringIO import StringIO +import certifi +import io from openpyxl import Workbook from openpyxl.styles import Alignment,Font @@ -16,15 +17,16 @@ ### function to go get the data def dtApiQuery(endpoint): - buffer=StringIO() + buffer=io.BytesIO() c = pycurl.Curl() c.setopt(c.URL, URL + endpoint) + c.setopt(pycurl.CAINFO, certifi.where()) c.setopt(c.HTTPHEADER, ['Authorization: Api-Token ' + APITOKEN] ) c.setopt(pycurl.WRITEFUNCTION, buffer.write) c.perform() print('Status: %d' % c.getinfo(c.RESPONSE_CODE)) c.close() - return(buffer) + return(buffer.getvalue().decode('UTF-8')) ### Setup workbook @@ -34,14 +36,13 @@ def dtApiQuery(endpoint): wsProcess = wb.create_sheet("processes") wsProcessProcess = wb.create_sheet("process-process") wsProcessHost = wb.create_sheet("process-host") -wb.remove_sheet(wb.active) +wb.remove(wb.active) ### Get & Process hosts data hostsIO=dtApiQuery('entity/infrastructure/hosts') -#print(hostsIO.getvalue()) -hosts=json.loads(hostsIO.getvalue()) +hosts=json.loads(hostsIO) wsHosts.append( ['hostId','displayName','osType','osVersion','hypervisorType','ipAddress1','ipAddress2','ipAddress3'] ) for host in hosts: @@ -67,8 +68,7 @@ def dtApiQuery(endpoint): ### Get & Process processes data processesIO=dtApiQuery('entity/infrastructure/processes') -#print(processesIO.getvalue()) -processes=json.loads(processesIO.getvalue()) +processes=json.loads(processesIO) wsProcess.append( ['processId','displayName','softwareType','softwareVersion','port1','port2','port3','port4','port5'] ) for process in processes: