-
Notifications
You must be signed in to change notification settings - Fork 34
/
dlipower.py
executable file
·585 lines (514 loc) · 18.5 KB
/
dlipower.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#!/usr/bin/python
# Copyright (c) 2009-2015, Dwight Hubbard
# Copyrights licensed under the New BSD License
# See the accompanying LICENSE.txt file for terms.
"""
Digital Loggers Web Power Switch Management
The module provides a python class named
powerswitch that allows managing the web power
switch from python programs.
When run as a script this acts as a command line utility to
manage the DLI Power switch.
Notes
-----
This module has been tested against the following
Digital Loggers Power network power switches:
WebPowerSwitch II
WebPowerSwitch III
WebPowerSwitch IV
WebPowerSwitch V
Ethernet Power Controller III
Examples
--------
*Connecting to a Digital Loggers Power switch*
>>> from dlipower import PowerSwitch
>>> switch = PowerSwitch(hostname='lpc.digital-loggers.com', userid='admin', password='4321')
*Getting the power state (status) from the switch*
Printing the switch object will print a table with the
Outlet Number, Name and Power State
>>> switch
DLIPowerSwitch at lpc.digital-loggers.com
Outlet Name State
1 Battery Charger OFF
2 K3 Power ON ON
3 Cisco Router OFF
4 WISP access poi ON
5 Shack Computer OFF
6 Router OFF
7 2TB Drive ON
8 Cable Modem1 ON
*Getting the name and powerswitch of the first outlet*
The PowerSwitch has a series of Outlet objects, they
will display their name and state if printed.
>>> switch[0]
<dlipower_outlet 'Traffic light:OFF'>
*Renaming the first outlet*
Changing the "name" attribute of an outlet will
rename the outlet on the powerswitch.
>>> switch[0].name = 'Battery Charger'
>>> switch[0]
<dlipower_outlet 'Battery Charger:OFF'>
*Turning the first outlet on*
Individual outlets can be accessed uses normal
list slicing operators.
>>> switch[0].on()
False
>>> switch[0]
<dlipower_outlet 'Battery Charger:ON'>
*Turning all outlets off*
The PowerSwitch() object supports iterating over
the available outlets.
>>> for outlet in switch:
... outlet.off()
...
False
False
False
False
False
False
False
False
>>> switch
DLIPowerSwitch at lpc.digital-loggers.com
Outlet Name State
1 Battery Charger OFF
2 K3 Power ON OFF
3 Cisco Router OFF
4 WISP access poi OFF
5 Shack Computer OFF
6 Router OFF
7 2TB Drive OFF
8 Cable Modem1 OFF
"""
import hashlib
import logging
import multiprocessing
import os
import json
import requests
import requests.exceptions
import time
import urllib3
from urllib.parse import quote
from bs4 import BeautifulSoup
logger = logging.getLogger(__name__)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Global settings
TIMEOUT = 20
RETRIES = 3
CYCLETIME = 3
CONFIG_DEFAULTS = {
'timeout': TIMEOUT,
'cycletime': CYCLETIME,
'userid': 'admin',
'password': '4321',
'hostname': '192.168.0.100'
}
CONFIG_FILE = os.path.expanduser('~/.dlipower.conf')
def _call_it(params): # pragma: no cover
"""indirect caller for instance methods and multiprocessing"""
instance, name, args = params
kwargs = {}
return getattr(instance, name)(*args, **kwargs)
class DLIPowerException(Exception):
"""
An error occurred talking the the DLI Power switch
"""
pass
class Outlet(object):
"""
A power outlet class
"""
use_description = True
def __init__(self, switch, outlet_number, description=None, state=None):
self.switch = switch
self.outlet_number = outlet_number
self.description = description
if not description:
self.description = str(outlet_number)
self._state = state
def __unicode__(self):
name = None
if self.use_description and self.description: # pragma: no cover
name = '%s' % self.description
if not name:
name = '%d' % self.outlet_number
return '%s:%s' % (name, self._state)
def __str__(self):
return self.__unicode__()
def __repr__(self):
return "<dlipower_outlet '%s'>" % self.__unicode__()
def _repr_html_(self): # pragma: no cover
""" Display representation as an html table when running in ipython """
return u"""<table>
<tr><th>Description</th><th>Outlet Number</th><th>State</th></tr>
<tr><td>{0:s}</td><td>{1:s}</td><td>{2:s}</td></tr>
</table>""".format(self.description, self.outlet_number, self.state)
@property
def state(self):
""" Return the outlet state """
return self._state
@state.setter
def state(self, value):
""" Set the outlet state """
self._state = value
if value in ['off', 'OFF', '0']:
self.off()
if value in ['on', 'ON', '1']:
self.on()
def off(self):
""" Turn the outlet off """
return self.switch.off(self.outlet_number)
def on(self):
""" Turn the outlet on """
return self.switch.on(self.outlet_number)
def rename(self, new_name):
"""
Rename the outlet
:param new_name: New name for the outlet
:return:
"""
return self.switch.set_outlet_name(self.outlet_number, new_name)
@property
def name(self):
""" Return the name or description of the outlet """
return self.switch.get_outlet_name(self.outlet_number)
@name.setter
def name(self, new_name):
""" Set the name of the outlet """
self.rename(new_name)
class PowerSwitch(object):
""" Powerswitch class to manage the Digital Loggers Web power switch """
__len = 0
login_timeout = 2.0
secure_login = False
def __init__(self, userid=None, password=None, hostname=None, timeout=None,
cycletime=None, retries=None, use_https=False):
"""
Class initializaton
"""
if not retries:
retries = RETRIES
config = self.load_configuration()
if retries:
self.retries = retries
if userid:
self.userid = userid
else:
self.userid = config['userid']
if password:
self.password = password
else:
self.password = config['password']
if hostname:
self.hostname = hostname
else:
self.hostname = config['hostname']
if timeout:
self.timeout = float(timeout)
else:
self.timeout = config['timeout']
if cycletime:
self.cycletime = float(cycletime)
else:
self.cycletime = config['cycletime']
self.scheme = 'http'
if use_https:
self.scheme = 'https'
self.base_url = '%s://%s' % (self.scheme, self.hostname)
self._is_admin = True
self.session = requests.Session()
self.login()
def __len__(self):
"""
:return: Number of outlets on the switch
"""
if self.__len == 0:
self.__len = len(self.statuslist())
return self.__len
def __repr__(self):
"""
display the representation
"""
if not self.statuslist():
return "Digital Loggers Web Powerswitch " \
"%s (UNCONNECTED)" % self.hostname
output = 'DLIPowerSwitch at %s\n' \
'Outlet\t%-15.15s\tState\n' % (self.hostname, 'Name')
for item in self.statuslist():
output += '%d\t%-15.15s\t%s\n' % (item[0], item[1], item[2])
return output
def _repr_html_(self):
"""
__repr__ in an html table format
"""
if not self.statuslist():
return "Digital Loggers Web Powerswitch " \
"%s (UNCONNECTED)" % self.hostname
output = '<table>' \
'<tr><th colspan="3">DLI Web Powerswitch at %s</th></tr>' \
'<tr>' \
'<th>Outlet Number</th>' \
'<th>Outlet Name</th>' \
'<th>Outlet State</th></tr>\n' % self.hostname
for item in self.statuslist():
output += '<tr><td>%d</td><td>%s</td><td>%s</td></tr>\n' % (
item[0], item[1], item[2])
output += '</table>\n'
return output
def __getitem__(self, index):
outlets = []
if isinstance(index, slice):
status = self.statuslist()[index.start:index.stop]
else:
status = [self.statuslist()[index]]
for outlet_status in status:
power_outlet = Outlet(
switch=self,
outlet_number=outlet_status[0],
description=outlet_status[1],
state=outlet_status[2]
)
outlets.append(power_outlet)
if len(outlets) == 1:
return outlets[0]
return outlets
def login(self):
self.secure_login = False
self.session = requests.Session()
try:
response = self.session.get(self.base_url, verify=False, timeout=self.login_timeout, allow_redirects=False)
if response.is_redirect:
self.base_url = response.headers['Location'].rstrip('/')
logger.debug(f'Redirecting to: {self.base_url}')
response = self.session.get(self.base_url, verify=False, timeout=self.login_timeout)
except (requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
self.session = None
return
soup = BeautifulSoup(response.text, 'html.parser')
fields = {}
for field in soup.find_all('input'):
name = field.get('name', None)
value = field.get('value', '')
if name:
fields[name] = value
fields['Username'] = self.userid
fields['Password'] = self.password
form_response = fields['Challenge'] + fields['Username'] + fields['Password'] + fields['Challenge']
m = hashlib.md5() # nosec - The switch we are talking to uses md5 hashes
m.update(form_response.encode())
data = {'Username': 'admin', 'Password': m.hexdigest()}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
try:
response = self.session.post('%s/login.tgi' % self.base_url, headers=headers, data=data, timeout=self.timeout, verify=False)
except requests.exceptions.ConnectTimeout:
self.secure_login = False
self.session = None
return
if response.status_code == 200:
if 'Set-Cookie' in response.headers:
self.secure_login = True
def load_configuration(self):
""" Return a configuration dictionary """
if os.path.isfile(CONFIG_FILE):
file_h = open(CONFIG_FILE, 'r')
try:
config = json.load(file_h)
except ValueError:
# Failed
return CONFIG_DEFAULTS
file_h.close()
return config
return CONFIG_DEFAULTS
def save_configuration(self):
""" Update the configuration file with the object's settings """
# Get the configuration from the config file or set to the defaults
config = self.load_configuration()
# Overwrite the objects configuration over the existing config values
config['userid'] = self.userid
config['password'] = self.password
config['hostname'] = self.hostname
config['timeout'] = self.timeout
# Write it to disk
file_h = open(CONFIG_FILE, 'w')
# Make sure the file perms are correct before we write data
# that can include the password into it.
if hasattr(os, 'fchmod'):
os.fchmod(file_h.fileno(), 0o0600)
if file_h:
json.dump(config, file_h, sort_keys=True, indent=4)
file_h.close()
else:
raise DLIPowerException('Unable to open configuration file for write')
def verify(self):
""" Verify we can reach the switch, returns true if ok """
if self.geturl():
return True
return False
def geturl(self, url='index.htm'):
"""
Get a URL from the userid/password protected powerswitch page Return None on failure
"""
full_url = "%s/%s" % (self.base_url, url)
result = None
request = None
logger.debug(f'Requesting url: {full_url}')
for i in range(0, self.retries):
try:
if self.secure_login and self.session:
request = self.session.get(full_url, timeout=self.timeout, verify=False, allow_redirects=True)
else:
request = requests.get(full_url, auth=(self.userid, self.password,), timeout=self.timeout, verify=False, allow_redirects=True) # nosec
except requests.exceptions.RequestException as e:
logger.warning("Request timed out - %d retries left.", self.retries - i - 1)
logger.exception("Caught exception %s", str(e))
continue
if request.status_code == 200:
result = request.content
break
logger.debug('Response code: %s', request.status_code)
logger.debug(f'Response content: {result}')
return result
def determine_outlet(self, outlet=None):
""" Get the correct outlet number from the outlet passed in, this
allows specifying the outlet by the name and making sure the
returned outlet is an int
"""
outlets = self.statuslist()
if outlet and outlets and isinstance(outlet, str):
for plug in outlets:
plug_name = plug[1]
if plug_name and plug_name.strip() == outlet.strip():
return int(plug[0])
try:
outlet_int = int(outlet)
if outlet_int <= 0 or outlet_int > self.__len__():
raise DLIPowerException('Outlet number %d out of range' % outlet_int)
return outlet_int
except ValueError:
raise DLIPowerException('Outlet name \'%s\' unknown' % outlet)
def get_outlet_name(self, outlet=0):
""" Return the name of the outlet """
outlet = self.determine_outlet(outlet)
outlets = self.statuslist()
if outlets and outlet:
for plug in outlets:
if int(plug[0]) == outlet:
return plug[1]
return 'Unknown'
def set_outlet_name(self, outlet=0, name="Unknown"):
""" Set the name of an outlet """
self.determine_outlet(outlet)
self.geturl(
url='unitnames.cgi?outname%s=%s' % (outlet, quote(name))
)
return self.get_outlet_name(outlet) == name
def off(self, outlet=0):
""" Turn off a power to an outlet
False = Success
True = Fail
"""
self.geturl(url='outlet?%d=OFF' % self.determine_outlet(outlet))
return self.status(outlet) != 'OFF'
def on(self, outlet=0):
""" Turn on power to an outlet
False = Success
True = Fail
"""
self.geturl(url='outlet?%d=ON' % self.determine_outlet(outlet))
return self.status(outlet) != 'ON'
def cycle(self, outlet=0):
""" Cycle power to an outlet
False = Power off Success
True = Power off Fail
Note, does not return any status info about the power on part of
the operation by design
"""
if self.off(outlet):
return True
time.sleep(self.cycletime)
self.on(outlet)
return False
def statuslist(self):
""" Return the status of all outlets in a list,
each item will contain 3 items plugnumber, hostname and state """
outlets = []
url = self.geturl('index.htm')
if not url:
return None
soup = BeautifulSoup(url, "html.parser")
# Get the root of the table containing the port status info
try:
root = soup.findAll('td', text='1')[0].parent.parent.parent
except IndexError:
# Finding the root of the table with the outlet info failed
# try again assuming we're seeing the table for a user
# account insteaed of the admin account (tables are different)
try:
self._is_admin = False
root = soup.findAll('th', text='#')[0].parent.parent.parent
except IndexError:
return None
for temp in root.findAll('tr'):
columns = temp.findAll('td')
if len(columns) == 5:
plugnumber = columns[0].string
hostname = columns[1].string
state = columns[2].find('font').string.upper()
outlets.append([int(plugnumber), hostname, state])
if self.__len == 0:
self.__len = len(outlets)
return outlets
def printstatus(self):
""" Print the status off all the outlets as a table to stdout """
if not self.statuslist():
print("Unable to communicate to the Web power switch at %s" % self.hostname)
return None
print('Outlet\t%-15.15s\tState' % 'Name')
for item in self.statuslist():
print('%d\t%-15.15s\t%s' % (item[0], item[1], item[2]))
return
def status(self, outlet=1):
"""
Return the status of an outlet, returned value will be one of:
ON, OFF, Unknown
"""
outlet = self.determine_outlet(outlet)
outlets = self.statuslist()
if outlets and outlet:
for plug in outlets:
if plug[0] == outlet:
return plug[2]
return 'Unknown'
def command_on_outlets(self, command, outlets):
"""
If a single outlet is passed, handle it as a single outlet and
pass back the return code. Otherwise run the operation on multiple
outlets in parallel the return code will be failure if any operation
fails. Operations that return a string will return a list of strings.
"""
if len(outlets) == 1:
result = getattr(self, command)(outlets[0])
if isinstance(result, bool):
return result
else:
return [result]
pool = multiprocessing.Pool(processes=len(outlets))
result = [
value for value in pool.imap(
_call_it,
[(self, command, (outlet, )) for outlet in outlets],
chunksize=1
)
]
pool.close()
pool.join()
if isinstance(result[0], bool):
for value in result:
if value:
return True
return result[0]
return result
if __name__ == "__main__": # pragma: no cover
PowerSwitch().printstatus()