Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame^] | 1 | __author__ = 'matt' |
| 2 | |
| 3 | from driver import PDUDriver |
| 4 | |
| 5 | class apc7952(PDUDriver): |
| 6 | def _pdu_logout(self): |
| 7 | self._back_to_main() |
| 8 | print("Logging out") |
| 9 | self.connection.send("4\r") |
| 10 | |
| 11 | def _back_to_main(self): |
| 12 | print("Returning to main menu") |
| 13 | self.connection.expect('>') |
| 14 | for i in range(1,20): |
| 15 | #print("Sending escape character") |
| 16 | self.connection.send("\x1B") |
| 17 | self.connection.send("\r") |
| 18 | res = self.connection.expect(["4- Logout","> "]) |
| 19 | if res == 0: |
| 20 | print("Back at main menu") |
| 21 | break |
| 22 | #self.connection.send("\r") |
| 23 | #self.connection.expect("4- Logout") |
| 24 | #self.connection.expect("> ") |
| 25 | |
| 26 | def _enter_outlet(self, outlet): |
| 27 | self.connection.expect("Press <ENTER> to continue...") |
| 28 | self.connection.send("\r") |
| 29 | self.connection.expect("> ") |
| 30 | self.connection.send(outlet) |
| 31 | self.connection.send("\r") |
| 32 | |
| 33 | def _port_interaction(self, command, port_number): |
| 34 | print("Attempting command: %s port: %i" % (command, port_number)) |
| 35 | ### make sure in main menu here |
| 36 | self._back_to_main() |
| 37 | self.connection.send("\r") |
| 38 | self.connection.expect("1- Device Manager") |
| 39 | self.connection.expect("> ") |
| 40 | self.connection.send("1\r") |
| 41 | res = self.connection.expect(["3- Outlet Control/Configuration","2- Outlet Control","2- Outlet Management"]) |
| 42 | if res == 0: |
| 43 | self.connection.send("3\r") |
| 44 | self._enter_outlet(port_number) |
| 45 | elif res == 1: |
| 46 | self.connection.send("2\r") |
| 47 | self._enter_outlet(port_number) |
| 48 | elif res == 2: |
| 49 | self.connection.send("2\r") |
| 50 | res = self.connection.expect(["1- Control Outlet", "1- Outlet Control/Configuration"]) |
| 51 | self.connection.expect("> ") |
| 52 | self.connection.send("1\r") |
| 53 | res = self.connection.expect(["> ","Press <ENTER> to continue..."]) |
| 54 | if res == 1: |
| 55 | print("Stupid paging thingmy detected, pressing enter") |
| 56 | self.connection.send("\r") |
| 57 | print("We should now be at the outlet list") |
| 58 | self.connection.send("%s\r" % port_number) |
| 59 | self.connection.send("1\r") |
| 60 | self.connection.expect("3- Immediate Reboot") |
| 61 | self.connection.expect("> ") |
| 62 | if command == "reboot": |
| 63 | self.connection.send("3\r") |
| 64 | self.connection.expect("Immediate Reboot") |
| 65 | self._do_it() |
| 66 | elif command == "delayed": |
| 67 | self.connection.send("6\r") |
| 68 | self.connection.expect("Delayed Reboot") |
| 69 | self._do_it() |
| 70 | elif command == "on": |
| 71 | self.connection.send("1\r") |
| 72 | self.connection.expect("Immediate On") |
| 73 | self._do_it() |
| 74 | elif command == "off": |
| 75 | self.connection.send("2\r") |
| 76 | self.connection.expect("Immediate Off") |
| 77 | self._do_it() |
| 78 | else: |
| 79 | print("Unknown command!") |
| 80 | |
| 81 | def _do_it(self): |
| 82 | self.connection.expect("Enter 'YES' to continue or <ENTER> to cancel :") |
| 83 | self.connection.send("YES\r") |
| 84 | self.connection.expect("Press <ENTER> to continue...") |
| 85 | self.connection.send("\r") |
| 86 | |
| 87 | def port_delayed(self, port_number): |
| 88 | self._port_interaction("delayed", port_number) |
| 89 | |
| 90 | def port_on(self, port_number): |
| 91 | self._port_interaction("on", port_number) |
| 92 | |
| 93 | def port_off(self, port_number): |
| 94 | self._port_interaction("off", port_number) |
| 95 | |
| 96 | def port_reboot(self, port_number): |
| 97 | self._port_interaction("reboot", port_number) |
| 98 | |
| 99 | class apc8959(PDUDriver): |
| 100 | connection = None |
| 101 | |
| 102 | def _pdu_logout(self): |
| 103 | print("logging out") |
| 104 | self.connection.send("\r") |
| 105 | self.connection.send("exit") |
| 106 | self.connection.send("\r") |
| 107 | print("done") |
| 108 | |
| 109 | def _pdu_get_to_prompt(self): |
| 110 | self.connection.send("\r") |
| 111 | self.connection.expect ('apc>') |
| 112 | |
| 113 | def _port_interaction(self, command, port_number): |
| 114 | print("Attempting %s on port %i" % (command, port_number)) |
| 115 | self._pdu_get_to_prompt() |
| 116 | self.connection.sendline(self.pdu_commands[command] + (" %i" % port_number)) |
| 117 | self.connection.expect("E000: Success") |
| 118 | print("done") |
| 119 | |
| 120 | def port_delayed(self, port_number): |
| 121 | self._port_interaction("delayed", port_number) |
| 122 | |
| 123 | def port_on(self, port_number): |
| 124 | self._port_interaction("on", port_number) |
| 125 | |
| 126 | def port_off(self, port_number): |
| 127 | self._port_interaction("off", port_number) |
| 128 | |
| 129 | def port_reboot(self, port_number): |
| 130 | self._port_interaction("reboot", port_number) |