Skip to content

Instantly share code, notes, and snippets.

@joenorton8014
Created August 30, 2017 14:15
Show Gist options
  • Save joenorton8014/d1cbd18d5e8fcc9bd235789bdd922fb6 to your computer and use it in GitHub Desktop.
Save joenorton8014/d1cbd18d5e8fcc9bd235789bdd922fb6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2.7
import qualysapi
import xmltodict
import datetime
import time
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
def main():
# Setup connection to QualysGuard API.
qgc = qualysapi.connect('/root/Documents/python/qualys/config.txt')
day = time.strftime("%Y%m%d_")
clock = time.strftime("%I%M%S")
timestamp = day+clock
date = '2017-08-20T14:07:17Z'
###################################################
# Different call types
###################################################
# API v2 call: Print out DNS name for a range of IPs.
host_call = '/api/2.0/fo/asset/host/'
vm_detection_call = '/api/2.0/fo/asset/host/vm/detection'
get_scans_call = '/api/2.0/fo/scan'
get_scan_report = '/msp/scan_report.php?ref='
# Get the scan reference number for the scan named "Weekly Scan Cycle" that ran in the last 7 days
scan_ref = findWeeklyScanRef(date)
report_url = get_scan_report + scan_ref
# Basic function for doing scan lookups. Kept in for reference
# Check page 32 here for paramters of scan listing = https://www.qualys.com/docs/qualys-api-v2-user-guide.pdf
def scanLookup():
scan_list_url = '/api/2.0/fo/scan'
parameters = {'action': 'list', 'scan_title': 'Weekly Scan Cycle'}
scan_list_output = qgc.request(scan_list_url, parameters)
return scan_list_output
# Find the scan reference number for the 'Weekly Scan Cycle' scan that ran in the last 7 days, eg: 'scan/1503324438.54589'
def findWeeklyScanRef(date):
scan_list_url = '/api/2.0/fo/scan'
parameters = {'action': 'list','launched_after_datetime': date}
qgc = qualysapi.connect('/root/Documents/python/qualys/config.txt')
scan_list_output = qgc.request(scan_list_url, parameters)
scans_dict = xmltodict.parse(scan_list_output)
number_of_scans = len(scans_dict['SCAN_LIST_OUTPUT']['RESPONSE']['SCAN_LIST']['SCAN'])
scan_range = range(0,(number_of_scans))
if scan_range < 1:
return False
else:
for scan_number in scan_range:
if '82188' in scans_dict['SCAN_LIST_OUTPUT']['RESPONSE']['SCAN_LIST']['SCAN'][scan_number]['TITLE']:
scan_ref = scans_dict['SCAN_LIST_OUTPUT']['RESPONSE']['SCAN_LIST']['SCAN'][scan_number]['REF']
return str(scan_ref)
def scanDownloadByRef(scan_ref):
scan_list_url = '/api/2.0/fo/scan'
parameters = {'action': 'fetch', 'scan_ref': scan_ref, 'output_format':'csv'}
scan_list_output = qgc.request(scan_list_url, parameters)
return scan_list_output
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment