131 lines
4.9 KiB
Python
Executable File
131 lines
4.9 KiB
Python
Executable File
#!/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)
|
|
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 + \
|
|
'"/>'
|
|
digest = hmac.new(self.secret_key.encode(), msg = data_tag.encode('utf-8')).hexdigest().upper()
|
|
xml = '<?xml version="1.0" ' \
|
|
'encoding="UTF-8" ' \
|
|
'standalone="yes"?>' \
|
|
'<setup>' + data_tag + \
|
|
'<auth code="' + digest + \
|
|
'"/></setup>'
|
|
return(xml)
|
|
def register(self,
|
|
ip_address,
|
|
shutdown_time,
|
|
delay_time,
|
|
hostname,
|
|
outlet_id,
|
|
bank_id,
|
|
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
|
|
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)
|
|
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']}
|
|
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)
|
|
|
|
u = cps_ups(address = configuration['UPS']['address'],
|
|
secret_key = configuration['UPS']['secret_key'],
|
|
port = int(configuration['UPS']['port']))
|
|
|
|
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'])
|