065cc28ac4f395bed902359d11fcdbf923e78541
[monitor.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 BayTechRPC16(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                 self.transport.open(self.host, self.username, None, "Enter user name:")
31                 self.transport.sendPassword(self.password, "Enter Password:")
32
33                 #self.transport.ifThenSend("RPC-16>", "Status")
34
35                 self.transport.ifThenSend("RPC-16>", "Reboot %d" % node_port)
36
37                 # Reboot Outlet  N        (Y/N)?
38                 if dryrun:
39                         self.transport.ifThenSend("(Y/N)?", "N")
40                 else:
41                         self.transport.ifThenSend("(Y/N)?", "Y")
42                 self.transport.ifThenSend("RPC-16>", "")
43
44                 self.transport.close()
45                 return 0
46
47 class BayTechCtrlCUnibe(PCUControl):
48         """
49                 For some reason, these units let you log in fine, but they hang
50                 indefinitely, unless you send a Ctrl-C after the password.  No idea
51                 why.
52         """
53         supported_ports = [22]
54         def run_ssh(self, node_port, dryrun):
55                 print "BayTechCtrlC %s" % self.host
56
57                 ssh_options="-o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no"
58                 s = pxssh.pxssh()
59                 if not s.login(self.host, self.username, self.password, ssh_options):
60                         raise ExceptionPassword("Invalid Password")
61                 # Otherwise, the login succeeded.
62
63                 # Send a ctrl-c to the remote process.
64                 print "sending ctrl-c"
65                 s.send(chr(3))
66
67                 # Control Outlets  (5 ,1).........5
68                 try:
69                         #index = s.expect("Enter Request")
70                         index = s.expect(["Enter Request :"])
71
72                         if index == 0:
73                                 print "3"
74                                 s.send("3\r\n")
75                                 time.sleep(5)
76                                 index = s.expect(["DS-RPC>", "Enter user name:"])
77                                 if index == 1:
78                                         s.send(self.username + "\r\n")
79                                         time.sleep(5)
80                                         index = s.expect(["DS-RPC>"])
81
82                                 if index == 0:
83                                         print "Reboot %d" % node_port
84                                         time.sleep(5)
85                                         s.send("Reboot %d\r\n" % node_port)
86
87                                         time.sleep(5)
88                                         index = s.expect(["\(Y/N\)\?", "Port in use", "DS-RPC>"])
89                                         if index == 0:
90                                                 if dryrun:
91                                                         print "sending N"
92                                                         s.send("N\r\n")
93                                                 else:
94                                                         print "sending Y"
95                                                         s.send("Y\r\n")
96                                         elif index == 1:
97                                                 raise ExceptionPrompt("PCU Reported 'Port in use.'")
98                                         elif index == 2:
99                                                 raise ExceptionSequence("Issued command 'Reboot' failed.")
100
101                                 time.sleep(5)
102                                 index = s.expect(["DS-RPC>"])
103                                 #print "got prompt back"
104
105                         s.close()
106
107                 except pexpect.EOF:
108                         raise ExceptionPrompt("EOF before expected Prompt")
109                 except pexpect.TIMEOUT:
110                         raise ExceptionPrompt("Timeout before expected Prompt")
111
112                 return 0
113
114 class BayTechCtrlC(PCUControl):
115         """
116                 For some reason, these units let you log in fine, but they hang
117                 indefinitely, unless you send a Ctrl-C after the password.  No idea
118                 why.
119         """
120         supported_ports = [22]
121         def run_ssh(self, node_port, dryrun):
122                 print "BayTechCtrlC %s" % self.host
123
124                 ssh_options="-o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no"
125                 s = pxssh.pxssh()
126                 if not s.login(self.host, self.username, self.password, ssh_options):
127                         raise ExceptionPassword("Invalid Password")
128                 # Otherwise, the login succeeded.
129
130                 # Send a ctrl-c to the remote process.
131                 print "SENDING ctrl-c"
132                 s.send(chr(3))
133
134                 # Control Outlets  (5 ,1).........5
135                 try:
136                         print "EXPECTING: ", "Enter Request :"
137                         index = s.expect(["Enter Request :"])
138
139                         if index == 0:
140                                 print "SENDING: 5"
141                                 s.send("5\r\n")
142                                 print "EXPECTING: ", "DS-RPC>"
143                                 index = s.expect(["DS-RPC>", "Enter user name:", "Port in use."])
144                                 if index == 1:
145                                         print "sending username"
146                                         s.send(self.username + "\r\n")
147                                         index = s.expect(["DS-RPC>"])
148                                 elif index == 2:
149                                         raise ExceptionPrompt("PCU Reported 'Port in use.'")
150
151                                 if index == 0:
152                                         print "SENDING: Reboot %d" % node_port
153                                         s.send("Reboot %d\r\n" % node_port)
154
155                                         print "SLEEPING: 5"
156                                         time.sleep(5)
157                                         print "EXPECTING: ", "Y/N?"
158                                         index = s.expect(["\(Y/N\)\?", "Port in use", "DS-RPC>"])
159                                         if index == 0:
160                                                 if dryrun:
161                                                         print "sending N"
162                                                         s.send("N\r\n")
163                                                 else:
164                                                         print "SENDING: Y"
165                                                         s.send("Y\r\n")
166                                         elif index == 1:
167                                                 raise ExceptionPrompt("PCU Reported 'Port in use.'")
168                                         elif index == 2:
169                                                 raise ExceptionSequence("Issued command 'Reboot' failed.")
170
171                                 # NOTE: for some reason, the script times out with the
172                                 # following line.  In manual tests, it works correctly, but
173                                 # with automated tests, evidently it fails.
174                                 print "SLEEPING: 5"
175                                 time.sleep(5)
176                                 #print "TOTAL--", s.allstr, "--EOT"
177                                 index = s.expect(["DS-RPC>"])
178                                 print "got prompt back"
179
180                         s.close()
181
182                 except pexpect.EOF:
183                         raise ExceptionPrompt("EOF before 'Enter Request' Prompt")
184                 except pexpect.TIMEOUT:
185                         raise ExceptionPrompt("Timeout before Prompt")
186
187                 return 0
188
189 class BayTech(PCUControl):
190         supported_ports = [22,23]
191
192         def run_telnet(self, node_port, dryrun):
193                 return self.run_ssh(node_port, dryrun)
194
195         def run_ssh(self, node_port, dryrun):
196                 self.transport.open(self.host, self.username)
197                 self.transport.sendPassword(self.password)
198
199                 # Control Outlets  (5 ,1).........5
200                 self.transport.ifThenSend("Enter Request :", "5")
201
202                 # Reboot N
203                 try:
204                         self.transport.ifThenSend("DS-RPC>", "Reboot %d" % node_port, ExceptionNotFound)
205                 except ExceptionNotFound, msg:
206                         # one machine is configured to ask for a username,
207                         # even after login...
208                         print "msg: %s" % msg
209                         self.transport.write(self.username + "\r\n")
210                         time.sleep(5)
211                         self.transport.ifThenSend("DS-RPC>", "Reboot %d" % node_port)
212
213                 # Reboot Outlet  N        (Y/N)?
214                 if dryrun:
215                         self.transport.ifThenSend("(Y/N)?", "N")
216                 else:
217                         self.transport.ifThenSend("(Y/N)?", "Y")
218                 time.sleep(5)
219                 self.transport.ifThenSend("DS-RPC>", "")
220
221                 self.transport.close()
222                 return 0