Setting tag pcucontrol-1.0-1
[pcucontrol.git] / pcucontrol / models / BayTech.py
1 from pcucontrol.reboot import *
2
3 class BayTechRPC3NC(PCUControl):
4         supported_ports = [22,23]
5         def run_telnet(self, node_port, dryrun):
6                 return self.run_ssh(node_port, dryrun)
7
8         def run_ssh(self, node_port, dryrun):
9                 self.transport.open(self.host, self.username, None, "Enter user name:")
10                 self.transport.sendPassword(self.password, "Enter Password:")
11
12                 #self.transport.ifThenSend("RPC-16>", "Status")
13                 self.transport.ifThenSend("RPC3-NC>", "Reboot %d" % node_port)
14
15                 # Reboot Outlet  N        (Y/N)?
16                 if dryrun:
17                         self.transport.ifThenSend("(Y/N)?", "N")
18                 else:
19                         self.transport.ifThenSend("(Y/N)?", "Y")
20                 self.transport.ifThenSend("RPC3-NC>", "")
21
22                 self.transport.close()
23                 return 0
24
25 class BayTechGeorgeTown(PCUControl):
26         supported_ports = [22,23]
27         def run_telnet(self, node_port, dryrun):
28                 return self.run_ssh(node_port, dryrun)
29         def run_ssh(self, node_port, dryrun):
30                 # NOTE: The georgetown pcu always drops the first connection, 
31                 self.transport.open(self.host, self.username, None, "Enter")
32                 self.transport.close()
33                 time.sleep(1)
34                 self.transport.open(self.host, self.username, None, "Enter user name:")
35                 self.transport.sendPassword(self.password, "Enter Password:")
36
37                 self.transport.ifThenSend("RPC-16>", "Reboot %d" % node_port)
38
39                 # Reboot Outlet  N        (Y/N)?
40                 if dryrun:
41                         self.transport.ifThenSend("(Y/N)?", "N")
42                 else:
43                         self.transport.ifThenSend("(Y/N)?", "Y")
44                 self.transport.ifThenSend("RPC-16>", "")
45
46                 self.transport.close()
47                 return 0
48
49
50 class BayTechRPC16(PCUControl):
51         supported_ports = [22,23]
52         def run_telnet(self, node_port, dryrun):
53                 return self.run_ssh(node_port, dryrun)
54         def run_ssh(self, node_port, dryrun):
55                 self.transport.open(self.host, self.username, None, "Enter user name:")
56                 self.transport.sendPassword(self.password, "Enter Password:")
57
58                 #self.transport.ifThenSend("RPC-16>", "Status")
59
60                 self.transport.ifThenSend("RPC-16>", "Reboot %d" % node_port)
61
62                 # Reboot Outlet  N        (Y/N)?
63                 if dryrun:
64                         self.transport.ifThenSend("(Y/N)?", "N")
65                 else:
66                         self.transport.ifThenSend("(Y/N)?", "Y")
67                 self.transport.ifThenSend("RPC-16>", "")
68
69                 self.transport.close()
70                 return 0
71
72 class BayTechCtrlCUnibe(PCUControl):
73         """
74                 For some reason, these units let you log in fine, but they hang
75                 indefinitely, unless you send a Ctrl-C after the password.  No idea
76                 why.
77         """
78         supported_ports = [22]
79         def run_ssh(self, node_port, dryrun):
80                 print "BayTechCtrlC %s" % self.host
81
82                 ssh_options="-o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no"
83                 s = pxssh.pxssh()
84                 if not s.login(self.host, self.username, self.password, ssh_options):
85                         raise ExceptionPassword("Invalid Password")
86                 # Otherwise, the login succeeded.
87
88                 # Send a ctrl-c to the remote process.
89                 print "sending ctrl-c"
90                 s.send(chr(3))
91
92                 # Control Outlets  (5 ,1).........5
93                 try:
94                         #index = s.expect("Enter Request")
95                         index = s.expect(["Enter Request :"])
96
97                         if index == 0:
98                                 print "3"
99                                 s.send("3\r\n")
100                                 time.sleep(5)
101                                 index = s.expect(["DS-RPC>", "Enter user name:"])
102                                 if index == 1:
103                                         s.send(self.username + "\r\n")
104                                         time.sleep(5)
105                                         index = s.expect(["DS-RPC>"])
106
107                                 if index == 0:
108                                         print "Reboot %d" % node_port
109                                         time.sleep(5)
110                                         s.send("Reboot %d\r\n" % node_port)
111
112                                         time.sleep(5)
113                                         index = s.expect(["\(Y/N\)\?", "Port in use", "DS-RPC>"])
114                                         if index == 0:
115                                                 if dryrun:
116                                                         print "sending N"
117                                                         s.send("N\r\n")
118                                                 else:
119                                                         print "sending Y"
120                                                         s.send("Y\r\n")
121                                         elif index == 1:
122                                                 raise ExceptionPrompt("PCU Reported 'Port in use.'")
123                                         elif index == 2:
124                                                 raise ExceptionSequence("Issued command 'Reboot' failed.")
125
126                                 time.sleep(5)
127                                 index = s.expect(["DS-RPC>"])
128                                 #print "got prompt back"
129
130                         s.close()
131
132                 except pexpect.EOF:
133                         raise ExceptionPrompt("EOF before expected Prompt")
134                 except pexpect.TIMEOUT:
135                         raise ExceptionPrompt("Timeout before expected Prompt")
136
137                 return 0
138
139 class BayTechCtrlC(PCUControl):
140         """
141                 For some reason, these units let you log in fine, but they hang
142                 indefinitely, unless you send a Ctrl-C after the password.  No idea
143                 why.
144         """
145         supported_ports = [22]
146         def run_ssh(self, node_port, dryrun):
147                 print "BayTechCtrlC %s" % self.host
148
149                 ssh_options="-o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no"
150                 s = pxssh.pxssh()
151                 try:
152                         if not s.login(self.host, self.username, self.password, ssh_options):
153                                 raise ExceptionPassword("Invalid Password")
154                 except pexpect.EOF:
155                         raise ExceptionNoTransport("No Connection Possible")
156                         
157                         
158                 # Otherwise, the login succeeded.
159
160                 # Send a ctrl-c to the remote process.
161                 print "SENDING ctrl-c"
162                 s.send(chr(3))
163
164                 # Control Outlets  (5 ,1).........5
165                 try:
166                         print "EXPECTING: ", "Enter Request :"
167                         index = s.expect(["Enter Request :"])
168
169                         if index == 0:
170                                 print "SENDING: 5"
171                                 s.send("5\r\n")
172                                 print "EXPECTING: ", "DS-RPC>"
173                                 index = s.expect(["DS-RPC>", "Enter user name:", "Port in use."])
174                                 if index == 1:
175                                         print "sending username"
176                                         s.send(self.username + "\r\n")
177                                         index = s.expect(["DS-RPC>"])
178                                 elif index == 2:
179                                         raise ExceptionPrompt("PCU Reported 'Port in use.'")
180
181                                 if index == 0:
182                                         print "SENDING: Reboot %d" % node_port
183                                         s.send("Reboot %d\r\n" % node_port)
184
185                                         print "SLEEPING: 5"
186                                         time.sleep(5)
187                                         print "EXPECTING: ", "Y/N?"
188                                         index = s.expect(["\(Y/N\)\?", "Port in use", "DS-RPC>"])
189                                         if index == 0:
190                                                 if dryrun:
191                                                         print "sending N"
192                                                         s.send("N\r\n")
193                                                 else:
194                                                         print "SENDING: Y"
195                                                         s.send("Y\r\n")
196                                         elif index == 1:
197                                                 raise ExceptionPrompt("PCU Reported 'Port in use.'")
198                                         elif index == 2:
199                                                 raise ExceptionSequence("Issued command 'Reboot' failed.")
200
201                                 # NOTE: for some reason, the script times out with the
202                                 # following line.  In manual tests, it works correctly, but
203                                 # with automated tests, evidently it fails.
204                                 print "SLEEPING: 5"
205                                 time.sleep(5)
206                                 #print "TOTAL--", s.allstr, "--EOT"
207                                 index = s.expect(["DS-RPC>"])
208                                 print "got prompt back"
209
210                         s.close()
211
212                 except pexpect.EOF:
213                         raise ExceptionPrompt("EOF before 'Enter Request' Prompt")
214                 except pexpect.TIMEOUT:
215                         raise ExceptionPrompt("Timeout before Prompt")
216
217                 return 0
218
219
220 class BayTech5CtrlC(PCUControl):
221         """
222                 For some reason, these units let you log in fine, but they hang
223                 indefinitely, unless you send a Ctrl-C after the password.  No idea
224                 why.
225         """
226         supported_ports = [22]
227         def run_ssh(self, node_port, dryrun):
228                 print "BayTech5CtrlC %s" % self.host
229
230                 ssh_options="-o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no"
231                 s = pxssh.pxssh()
232                 try:
233                         if not s.login(self.host, self.username, self.password, ssh_options):
234                                 raise ExceptionPassword("Invalid Password")
235                 except pexpect.EOF:
236                         raise ExceptionNoTransport("No Connection Possible")
237                         
238                         
239                 # Otherwise, the login succeeded.
240                 # Control Outlets  (5 ,1).........5
241                 try:
242                         print "EXPECTING: ", "Enter Request :"
243                         s.send("\r\n")
244                         time.sleep(2)
245                         index = s.expect(["Enter Request"])
246
247                         if index == 0:
248                                 print "SENDING: 5"
249                                 s.send("5\r\n")
250                                 print "EXPECTING: ", "DS-RPC>"
251                                 time.sleep(3)
252                                 # Send a ctrl-c to the remote process.
253                                 #print "SENDING ctrl-c"
254                                 #s.send(chr(3))
255
256                                 index = s.expect(["DS-RPC>", "Enter user name:", "Port in use."])
257                                 if index == 1:
258                                         print "sending username"
259                                         s.send(self.username + "\r\n")
260                                         index = s.expect(["DS-RPC>"])
261                                 elif index == 2:
262                                         raise ExceptionPrompt("PCU Reported 'Port in use.'")
263
264                                 if index == 0:
265                                         print "SENDING: Reboot %d" % node_port
266                                         #s.send("Reboot %d\r\n" % node_port)
267                                         s.send("Reboot %d\r" % node_port)
268
269                                         print "SLEEPING: 5"
270                                         time.sleep(5)
271                                         print "EXPECTING: ", "Y/N?"
272                                         index = s.expect(["\(Y/N\)\?", "Port in use", "DS-RPC>"])
273                                         if index == 0:
274                                                 if dryrun:
275                                                         print "sending N"
276                                                         s.send("N\r\n")
277                                                 else:
278                                                         print "SENDING: Y"
279                                                         s.send("Y\r\n")
280                                         elif index == 1:
281                                                 raise ExceptionPrompt("PCU Reported 'Port in use.'")
282                                         elif index == 2:
283                                                 raise ExceptionSequence("Issued command 'Reboot' failed.")
284
285                                 # NOTE: for some reason, the script times out with the
286                                 # following line.  In manual tests, it works correctly, but
287                                 # with automated tests, evidently it fails.
288                                 print "SLEEPING: 5"
289                                 time.sleep(5)
290                                 #print "TOTAL--", s.allstr, "--EOT"
291                                 index = s.expect(["DS-RPC>"])
292                                 print "got prompt back"
293
294                         s.close()
295
296                 except pexpect.EOF:
297                         raise ExceptionPrompt("EOF before 'Enter Request' Prompt")
298                 except pexpect.TIMEOUT:
299                         raise ExceptionPrompt("Timeout before Prompt")
300
301                 return 0
302
303 class BayTech(PCUControl):
304         supported_ports = [22,23]
305
306         def run_telnet(self, node_port, dryrun):
307                 return self.run_ssh(node_port, dryrun)
308
309         def run_ssh(self, node_port, dryrun):
310                 self.transport.open(self.host, self.username)
311                 self.transport.sendPassword(self.password)
312
313                 # Control Outlets  (5 ,1).........5
314                 self.transport.ifThenSend("Enter Request :", "5")
315
316                 # Reboot N
317                 try:
318                         self.transport.ifThenSend("DS-RPC>", "Reboot %d" % node_port, ExceptionNotFound)
319                 except ExceptionNotFound, msg:
320                         # one machine is configured to ask for a username,
321                         # even after login...
322                         print "msg: %s" % msg
323                         self.transport.write(self.username + "\r\n")
324                         time.sleep(5)
325                         self.transport.ifThenSend("DS-RPC>", "Reboot %d" % node_port)
326
327                 # Reboot Outlet  N        (Y/N)?
328                 if dryrun:
329                         self.transport.ifThenSend("(Y/N)?", "N")
330                 else:
331                         self.transport.ifThenSend("(Y/N)?", "Y")
332                 time.sleep(5)
333                 self.transport.ifThenSend("DS-RPC>", "")
334
335                 self.transport.close()
336                 return 0