108 lines
3.8 KiB
Python
Executable File
108 lines
3.8 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>')
|
|
|
|
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)
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read(configfile)
|
|
|
|
u = cps_ups(address = config.get('UPS', 'address'),
|
|
secret_key = config.get('UPS','secret_key'),
|
|
port = int(config.get('UPS', 'port', fallback='3052')))
|
|
|
|
u.register(ip_address = config.get('HOST','ip_address'),
|
|
shutdown_time = config.get('HOST','shutdown_time'),
|
|
delay_time = config.get('HOST','delay_time'),
|
|
hostname = config.get('HOST','hostname'),
|
|
outlet_id = config.get('HOST','outlet_id'),
|
|
bank_id = config.get('HOST','bank_id'),
|
|
isps = config.get('HOST','isps'),
|
|
contact = config.get('HOST','contact'),
|
|
location = config.get('HOST','location'),
|
|
bedt = config.get('HOST','bedt'))
|