-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtripplite.py
executable file
·262 lines (219 loc) · 7.63 KB
/
tripplite.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
#!/usr/bin/env python
"""
Tripp Lite Power Distribution Unit (PDU) control using python script
Written by Jay Schulist <[email protected]>
Version 0.1.1
Date 06/18/2019
./tripplite.py -h [hostname] {-u [username] -p [password]} --[command]
-h : hostname of the PDU unit to login
-u : username to use for login (optional then default used)
-p : Password to use for login (optional then default used)
--[command] : one of the following
--status
--reboot
--force
--cycle=[all|outlet_num]
--on=[all|outlet_num]
--off=[all|outlet_num]
Example(s):
Cycle all outlets on the given PDU using default login credentials:
python tripplite.py -h pdu-e01 --reboot
Force a power off, sleep, then power on using default login credentials:
python tripplite.py -h pdu-e02 --force
Print information about the given PDU:
python tripplite.py -h pdu-e04 -u localadmin -p localadmin --status
Cycle all outlets on the given PDU:
python tripplite.py -h pdu-e04 -u localadmin -p localadmin --cycle=all
Power off all outlets on the given PDU:
python tripplite.py -h pdu-e04 -u localadmin -p localadmin --off=all
Power on all outlets on the given PDU:
python tripplite.py -h pdu-e04 -u localadmin -p localadmin --on=all
Log output of command execution located at /tmp/tripplite.log
"""
import getopt
# import getpass
import os
# import re
import sys
import time
import pexpect
class Tripplite(object):
COMMAND_PROMPT = '>> '
COMMAND_TIMEOUT = 100
LOGFILE = '/tmp/tripplite.log'
def __init__(self, hostname, username='localadmin', password='localadmin'):
self.hostname = hostname
self.username = username
self.password = password
def connect(self):
# Login via telnet (SSH doesn't work with pexpect)
try:
self.tel = pexpect.spawn(f'telnet {self.hostname}', encoding='utf-8')
except:
raise Exception(f'Unable to connect to PDU {self.hostname}')
try:
self.tel.logfile = open(self.LOGFILE, 'w')
self.tel.expect('^.* login: ', timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('%s' % self.username)
self.tel.expect('Password: ', timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('%s' % self.password)
except:
raise Exception('Unable to login')
# Now at the devices command prompt and ready to run some commands.
self.tel.expect(self.COMMAND_PROMPT)
self.tel.sendline('1')
def status(self):
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('1')
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
output_status = self.tel.before[92:-75]
return output_status
def cycle(self, port='all'):
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('3')
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('6')
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('Y')
return True
def on(self, port='all'):
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('3')
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('9')
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('Y')
return True
def off(self, port='all'):
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('3')
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('5')
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('Y')
return True
def close(self):
# Now exit the remote host and close the connection.
self.tel.sendline('M')
self.tel.expect(self.COMMAND_PROMPT, timeout=self.COMMAND_TIMEOUT)
self.tel.sendline('Q')
self.tel.close()
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def __repr__(self):
return f'PDU connect by telnet to {self.username}@{self.hostname}'
def exit_with_usage():
print(globals()['__doc__'])
os._exit(1)
def main():
force_timeout = 10 * 60
# Parse the options, arguments
try:
optlist, args = getopt.getopt(sys.argv[1:], 'h:u:p:', ['status', 'cycle=', 'on=', 'off=', 'force', 'reboot'])
except Exception as e:
print(str(e))
exit_with_usage()
options = dict(optlist)
if len(args) > 3:
exit_with_usage()
# print(options)
if '-h' in options:
hostname = options['-h']
else:
hostname = input('hostname: ')
if '-u' in options:
username = options['-u']
else:
username = 'localadmin'
print(f'Using default username: {username}')
if '-p' in options:
password = options['-p']
else:
password = 'localadmin'
print(f'Using default password: {password}')
# takes one command per execution, the first match
# --status
# --force
# --reboot
# --[cycle|power_on|power_off]=[all|1-24]
status = False
cycle = None
power_on = None
power_off = None
force = False
reboot = False
if '--status' in options:
status = True
elif '--cycle' in options:
cycle = options['--cycle']
elif '--on' in options:
power_on = options['--on']
elif '--off' in options:
power_off = options['--off']
elif '--force' in options:
force = True
elif '--reboot' in options:
reboot = True
else:
exit_with_usage()
pdu = Tripplite(hostname, username, password)
print(pdu)
pdu.connect()
# status
if status:
print(f'Status: {hostname}')
output_status = pdu.status()
print(output_status)
elif reboot:
print(f'Rebooting ALL loads on {hostname}... ', end='')
sys.stdout.flush()
pdu.cycle()
print('done')
elif force:
print(f'Force off ALL loads on {hostname}... ', end='')
sys.stdout.flush()
pdu.off()
pdu.close()
print('done')
print('Sleeping for %d minutes... ' % (force_timeout / 60), end='')
sys.stdout.flush()
time.sleep(force_timeout)
print('done')
pdu.connect()
print(f'Force on ALL loads on {hostname}... ', end='')
sys.stdout.flush()
pdu.on()
print('done')
# cycle outlets
elif cycle is not None:
if 'all' in cycle:
# cycle all loads
print(f'Cycling ALL loads on {hostname}... ', end='')
sys.stdout.flush()
pdu.cycle()
print('done')
else:
# cycle specific outlet
print(f'Cycle load on outlet: {cycle}')
elif power_on is not None:
if 'all' in power_on:
# power on all loads
print(f'Power on ALL loads on {hostname}... ', end='')
sys.stdout.flush()
pdu.on()
print('done')
else:
# power on specific outlet
print("Power on outlet")
elif power_off is not None:
if 'all' in power_off:
# power off all loads
print(f'Power off ALL loads on {hostname}... ', end='')
sys.stdout.flush()
pdu.off()
print('done')
else:
# power off specific outlet
print("Power off outlet")
if __name__ == "__main__":
main()