cyberpower/registerups/registerups.py

141 lines
5.2 KiB
Python
Raw Normal View History

2019-08-26 02:39:31 +03:00
#!/usr/bin/env python
import sys
import os.path
import configparser
import http.client
import xml.etree.ElementTree as ET
import hmac
class cps_ups:
def __init__(self, address, secret_key, port = 3052):
self.address = address
self.port = port
self.secret_key = secret_key
self.connection = http.client.HTTPConnection(self.address, self.port)
def __get_keepalive_data__(self):
self.connection.request("GET", "/keepalive.xml?ip=" + self.host_address + "&id=-1&type=1")
for i in ET.fromstring(self.connection.getresponse().read()).iter("data"):
keepalive_data=i.attrib
self.connection.close()
return(keepalive_data)
2019-08-26 02:39:31 +03:00
def __genxml__(self, keepalive_data):
data_tag = '<data ver="1.1"'\
'hash="hmac-md5-128" '\
'boot="' + keepalive_data['boot'] + \
'" upt="' + keepalive_data['upt'] + \
'" ip="' + self.host_address + \
'" id="' + keepalive_data['id'] + \
'" st="' + self.shutdown_time + \
'" dt="' + self.delay_time + \
'" bedt="' + self.bedt + \
'" name="' + self.hostname + \
'" loc="' + self.location + \
'" cont="' + self.contact + \
'" oid="' + self.outlet_id + \
'" isps="' + self.isps + \
'" bid="' + self.bank_id + \
'" sa="' + self.sa + \
'" event="' + self.event + \
2019-08-26 02:39:31 +03:00
'"/>'
digest = hmac.new(self.secret_key.encode(), msg = data_tag.encode('utf-8')).hexdigest().upper()
2019-08-26 13:23:35 +03:00
xml = '<?xml version="1.0" ' \
'encoding="UTF-8" ' \
'standalone="yes"?>' \
'<setup>' + data_tag + \
'<auth code="' + digest + \
'"/></setup>'
2019-08-26 02:39:31 +03:00
return(xml)
def register(self,
ip_address,
shutdown_time,
delay_time,
hostname,
outlet_id,
bank_id,
sa,
event,
2019-08-26 02:39:31 +03:00
isps = "true",
contact = "",
location = "",
bedt = "0", ):
self.host_address = ip_address
self.shutdown_time = shutdown_time
self.delay_time = delay_time
self.hostname = hostname
self.outlet_id = outlet_id
self.bank_id = bank_id
self.isps = isps
self.contact = contact
self.location = location
self.bedt = bedt
self.sa = sa
self.event = event
try:
post_data = self.__genxml__(self.__get_keepalive_data__())
self.connection.request("POST", "/setup.xml", body = post_data)
except (OSError, ConnectionError) as err:
print('Unable to register: ' + str(err))
sys.exit(3)
2019-08-26 02:39:31 +03:00
response = self.connection.getresponse().read()
self.connection.close()
return(response)
def help():
print('Usage: ' + sys.argv[0] + ' <path-to-configuration-file>')
configuration_structure = {'UPS': ['address',
'secret_key',
'port'],
'HOST': ['ip_address',
'shutdown_time',
'delay_time',
'hostname',
'outlet_id',
'bank_id',
'isps',
'contact',
'location',
'bedt',
'sa',
'event']}
2019-08-26 02:39:31 +03:00
try:
configfile = sys.argv[1]
except IndexError:
help()
sys.exit(1)
if not os.path.isfile(configfile):
print('Error: configuration file ' + configfile + ' doesn\'t exists')
sys.exit(2)
configuration = {}
try:
config = configparser.ConfigParser()
config.read(configfile)
for section in configuration_structure:
configuration[section] = {}
for param in configuration_structure[section]:
configuration[section][param] = config.get(section, param)
except (configparser.NoOptionError,
configparser.MissingSectionHeaderError,
configparser.ParsingError,
configparser.NoSectionError) as err:
print('Configuration file parsing error:\n' + str(err))
sys.exit(3)
2019-08-26 02:39:31 +03:00
u = cps_ups(address = configuration['UPS']['address'],
secret_key = configuration['UPS']['secret_key'],
port = int(configuration['UPS']['port']))
2019-08-26 02:39:31 +03:00
u.register(ip_address = configuration['HOST']['ip_address'],
shutdown_time = configuration['HOST']['shutdown_time'],
delay_time = configuration['HOST']['delay_time'],
hostname = configuration['HOST']['hostname'],
outlet_id = configuration['HOST']['outlet_id'],
bank_id = configuration['HOST']['bank_id'],
isps = configuration['HOST']['isps'],
contact = configuration['HOST']['contact'],
location = configuration['HOST']['location'],
bedt = configuration['HOST']['bedt'],
sa = configuration['HOST']['sa'],
event = configuration['HOST']['event'])