5883c4b03fc3a69e7caff24b49d67e211616995e
[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                 try:
127                         if not s.login(self.host, self.username, self.password, ssh_options):
128                                 raise ExceptionPassword("Invalid Password")
129                 except pexpect.EOF:
130                         raise ExceptionNoTransport("No Connection Possible")
131                         
132                         
133                 # Otherwise, the login succeeded.
134
135                 # Send a ctrl-c to the remote process.
136                 print "SENDING ctrl-c"
137                 s.send(chr(3))
138
139                 # Control Outlets  (5 ,1).........5
140                 try:
141                         print "EXPECTING: ", "Enter Request :"
142                         index = s.expect(["Enter Request :"])
143
144                         if index == 0:
145                                 print "SENDING: 5"
146                                 s.send("5\r\n")
147                                 print "EXPECTING: ", "DS-RPC>"
148                                 index = s.expect(["DS-RPC>", "Enter user name:", "Port in use."])
149                                 if index == 1:
150                                         print "sending username"
151                                         s.send(self.username + "\r\n")
152                                         index = s.expect(["DS-RPC>"])
153                                 elif index == 2:
154                                         raise ExceptionPrompt("PCU Reported 'Port in use.'")
155
156                                 if index == 0:
157                                         print "SENDING: Reboot %d" % node_port
158                                         s.send("Reboot %d\r\n" % node_port)
159
160                                         print "SLEEPING: 5"
161                                         time.sleep(5)
162                                         print "EXPECTING: ", "Y/N?"
163                                         index = s.expect(["\(Y/N\)\?", "Port in use", "DS-RPC>"])
164                                         if index == 0:
165                                                 if dryrun:
166                                                         print "sending N"
167                                                         s.send("N\r\n")
168                                                 else:
169                                                         print "SENDING: Y"
170                                                         s.send("Y\r\n")
171                                         elif index == 1:
172                                                 raise ExceptionPrompt("PCU Reported 'Port in use.'")
173                                         elif index == 2:
174                                                 raise ExceptionSequence("Issued command 'Reboot' failed.")
175
176                                 # NOTE: for some reason, the script times out with the
177                                 # following line.  In manual tests, it works correctly, but
178                                 # with automated tests, evidently it fails.
179                                 print "SLEEPING: 5"
180                                 time.sleep(5)
181                                 #print "TOTAL--", s.allstr, "--EOT"
182                                 index = s.expect(["DS-RPC>"])
183                                 print "got prompt back"
184
185                         s.close()
186
187                 except pexpect.EOF:
188                         raise ExceptionPrompt("EOF before 'Enter Request' Prompt")
189                 except pexpect.TIMEOUT:
190                         raise ExceptionPrompt("Timeout before Prompt")
191
192                 return 0
193
194 class BayTech(PCUControl):
195         supported_ports = [22,23]
196
197         def run_telnet(self, node_port, dryrun):
198                 return self.run_ssh(node_port, dryrun)
199
200         def run_ssh(self, node_port, dryrun):
201                 self.transport.open(self.host, self.username)
202                 self.transport.sendPassword(self.password)
203
204                 # Control Outlets  (5 ,1).........5
205                 self.transport.ifThenSend("Enter Request :", "5")
206
207                 # Reboot N
208                 try:
209                         self.transport.ifThenSend("DS-RPC>", "Reboot %d" % node_port, ExceptionNotFound)
210                 except ExceptionNotFound, msg:
211                         # one machine is configured to ask for a username,
212                         # even after login...
213                         print "msg: %s" % msg
214                         self.transport.write(self.username + "\r\n")
215                         time.sleep(5)
216                         self.transport.ifThenSend("DS-RPC>", "Reboot %d" % node_port)
217
218                 # Reboot Outlet  N        (Y/N)?
219                 if dryrun:
220                         self.transport.ifThenSend("(Y/N)?", "N")
221                 else:
222                         self.transport.ifThenSend("(Y/N)?", "Y")
223                 time.sleep(5)
224                 self.transport.ifThenSend("DS-RPC>", "")
225
226                 self.transport.close()
227                 return 0