-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrpc3Control.py
executable file
·159 lines (120 loc) · 4.32 KB
/
rpc3Control.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
#!/usr/bin/python
"""
Control Class for the Baytech RPC3
J. Adams <[email protected]>
"""
from pexpect import *
import sys
import re
import syslog
class rpc3ControlError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class rpc3Control:
""" Class to control a Baytech RPC-3 """
child = None
status = {}
name = {}
statuscached = False
unitid = ""
def __init__(self, hostname, user=None, password=None, debug=False):
self.hostname = hostname
self.user = user
self.password = password
self.debug = debug
self.connect()
self.unitid = ""
"""
the RPC supports username, or password, or both
so we support both of those cases in this code.
"""
""" get unit id """
self.child.expect([".*Unit ID: (.*)", EOF, TIMEOUT])
self.unitid = self.child.match.group(1)
if user != None:
self.es("Enter username>", user)
if password != None:
self.es("Enter password>", password)
def es(self,str_expect,str_send):
"""
a pexpect helper method; expect and send with error monitoring
"""
result = self.child.expect([str_expect, EOF, TIMEOUT])
if result == 0:
self.child.send("%s\r" % str_send)
return
if result > 0:
if result == 1:
raise rpc3ControlError("EOF during read")
else:
raise rpc3ControlError("Timeout during read")
def connect(self):
if self.child == None:
self.child = spawn("telnet " + self.hostname)
result = self.child.expect(["Connected to", EOF, TIMEOUT])
self.child.send("\r")
if self.debug == True:
self.child.logfile = sys.stdout
def outlet(self,outlet_number,state):
"""
control an outlet
state is one of "on","off",or "reboot"
outlet_number is an integer.
"""
if state not in ("on", "off", "reboot"):
raise rpc3ControlError('Invalid outlet state')
if int(outlet_number) > 8 or int(outlet_number) < 1:
return None
self.es("Enter Selection>", "1")
self.es("RPC-3>", "%s %d\rY" % (state, outlet_number) )
self.es("RPC-3>", "MENU")
self.statuscached = False
return True
def outlet_status(self, outlet_number, ignore_cache = False):
""" Get the status of an outlet """
if self.statuscached == True and ignore_cache != True:
return (self.status[outlet_number],self.name[outlet_number])
if int(outlet_number) > 8 or int(outlet_number) < 1:
return None
self.es("Enter Selection>", "1")
self.es("RPC-3>", "MENU")
# parse the output
inlist = False
for line in self.child.before.split('\n'):
if line.rstrip() == "" and inlist:
inlist = False
if inlist:
m = re.match('^ .....([0-9]) ..... (...........) ([0-9]) ..... (On|Off)',line)
self.name[int(m.group(1))] = m.group(2).rstrip()
if m.group(4) == "On":
self.status[int(m.group(1))] = True
else:
self.status[int(m.group(1))] = False
if line.find("Status") != -1:
inlist = True
self.statuscached = True
return (self.status[outlet_number],self.name[outlet_number])
# fetch credentials
# the credentials should be in a file called ".credentials" and in the form "hostname:user:pass" on one line.
def load_credentials(credentials = ".credentials" ):
user = None
pw = None
rpc = None
err = None
whitelist = None
try:
f = open(credentials, 'r')
(rpc, user, pw, w) = f.readline().rstrip().split(":",3)
whitelist = w.split(",")
f.close()
except IOError:
err = "FATAL: Couldn't open credentials file"
except ValueError:
err = "FATAL: Malformed %s file. Credentails should be in the form \"host:user:pw\"" % credentials
if err != None:
syslog.syslog(syslog.LOG_ERR, err)
print >> sys.stderr, err
sys.exit(1)
return (rpc,user,pw,whitelist)