#!/usr/bin/python -tt import sys import urllib2, urllib import optparse import re #BEGIN_VERSION_GENERATION RELEASE_VERSION="Fence agent for Intel AMT v1.x" REDHAT_COPYRIGHT="" BUILD_DATE="" #END_VERSION_GENERATION RE_STATUS = re.compile("(On|Off)", re.IGNORECASE) def set_power_status(status): actions = {"on" : "2", "off" : "1", "cycle" : "3", "reset" : "4"} send_command(options.cluster,options.port,options.login,options.password,"POST",actions[status]) def get_power_status(): html = send_command(options.cluster,options.port+"/MainInfo",options.login,options.password) state = RE_STATUS.search(html) shw = "Node status: " + state.group(1) return shw def send_command(ip,port,Username,Password,method="GET",command=""): if ip[:7] == "http://": URL = ip + ":" + port else:URL = "http://" + ip + ":" + port passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, URL, Username, Password) # use HTTPDigestAuthHandler instead here authhandler = urllib2.HTTPDigestAuthHandler(passman) opener = urllib2.build_opener(authhandler) if method == 'POST': values = {'pt_html_rc_RadioGroup' : command, 'pt_html_rc_boot_special' : 'Normal boot', 'Submit' : 'Send Command'} data = urllib.urlencode(values) req = urllib2.Request(URL + "/pt_html_rc", data) res = opener.open(req) res = opener.open(URL) return res.read() res.close() def main(): parser = optparse.OptionParser(usage="usage: %prog [options]") parser.add_option("-i", "--ip", dest="cluster", type="string", help="Type an ip of a node") parser.add_option("-P","--port", dest="port", type="string", help="Type a port of a node") parser.add_option("-s","--setstatus", dest="status", type="string", help="Set cluster's status (on, off, reset)") parser.add_option("-p","--pwd", dest="password", type="string", help="Enter password") parser.add_option("-l","--login", dest="login", type="string", help="Enter login") parser.add_option("-g", "--get", dest="getstatus", type="string", default="get", help="Get power status of a node") global options options, args = parser.parse_args() if options.status: set_power_status(options.status) print(get_power_status()) elif options.getstatus: print(get_power_status()) else:parser.print_help() if __name__ == "__main__": main()