Skip to content

Instantly share code, notes, and snippets.

@sideangleside
Created December 7, 2016 10:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sideangleside/dda140ee5f4d3097e3081d8eefc2adff to your computer and use it in GitHub Desktop.
Save sideangleside/dda140ee5f4d3097e3081d8eefc2adff to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# File: showErrataReqReboot.py
# Author: Rich Jerrido <rjerrido@outsidaz.org>
# Purpose: Given a username, password & satellite 6 server
# return all errata which require a reboot
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import json
import getpass
import os
import urllib2
import urllib
import base64
import sys
import ssl
import csv
from optparse import OptionParser
# Set these if you wish 'hardcode' the values
# And not be prompted for them.
default_login = None
default_password = None
default_satellite = None
parser = OptionParser()
parser.add_option("-l", "--login", dest="login", help="Login user", metavar="LOGIN", default=default_login)
parser.add_option("-p", "--password", dest="password", help="Password for specified user. Will prompt if omitted", metavar="PASSWORD", default=default_password)
parser.add_option("-s", "--satellite", dest="satellite", help="FQDN of Satellite - omit https://", metavar="SATELLITE", default=default_satellite)
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="Verbose output")
(options, args) = parser.parse_args()
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
if not (options.login and options.satellite):
print "Must specify login & satellite options. See usage:"
parser.print_help()
print "\nExample usage: ./showErrataReqReboot.py -l admin -s satellite.example.com"
sys.exit(1)
else:
login = options.login
password = options.password
satellite = options.satellite
if not password:
password = getpass.getpass("%s's password:" % login)
erratadata = []
try:
page = 0
per_page = 100
while (page == 0 or int(jsonresult['per_page']) == len(jsonresult['results'])):
page += 1
if options.verbose:
print 'Retrieving page %s ' % page
q = [('page', page), ('per_page', per_page)]
url = "https://" + satellite + "/katello/api/v2/errata?" + urllib.urlencode(q)
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (login, password)).strip()
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
jsonresult = json.load(result)
total_errata = jsonresult['total']
erratadata += jsonresult['results']
except urllib2.URLError, e:
print "Error: cannot connect to the API: %s" % (e)
print "Check your URL & try to login using the same user/pass via the WebUI and check the error!"
sys.exit(1)
except Exception, e:
print "FATAL Error - %s" % (e)
sys.exit(2)
if options.verbose:
print 'Found %s total errata' % total_errata
print 'Now listing those errata which suggest a reboot'
print 'Errata ID, Errata Title, Reboot Suggested'
for errata in erratadata:
if errata['reboot_suggested'] is True:
print '%s,%s,%s' % (errata['errata_id'],errata['title'],errata['reboot_suggested'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment