matthew.hart@linaro.org | 5e4fce9 | 2013-08-22 11:29:21 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2013 Linaro Limited |
| 4 | # Author Matt Hart <matthew.hart@linaro.org> |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | # MA 02110-1301, USA. |
| 20 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 21 | import pexpect |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 22 | import os |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 23 | import logging |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 24 | import sys |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 25 | |
Matt Hart | d73bba2 | 2013-08-21 13:46:03 +0100 | [diff] [blame] | 26 | from apcdrivers import apc8959 |
| 27 | from apcdrivers import apc7952 |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 28 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 29 | |
| 30 | class PDUEngine(): |
| 31 | connection = None |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 32 | prompt = 0 |
| 33 | driver = None |
| 34 | |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame^] | 35 | def __init__(self, pdu_hostname, pdu_telnetport=23): |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 36 | self.exec_string = "/usr/bin/telnet %s %d" % (pdu_hostname, pdu_telnetport) |
| 37 | logging.debug("Created new PDUEngine: %s" % self.exec_string) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 38 | #self.connection.logfile_read = sys.stdout |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame^] | 39 | prompt = self._pdu_login("apc", "apc") |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 40 | if prompt == 0: |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 41 | logging.debug("Found a v5 prompt") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 42 | self.driver = apc8959(self.connection) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 43 | elif prompt == 1: |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 44 | logging.debug("Found a v3 prompt") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 45 | self.driver = apc7952(self.connection) |
| 46 | else: |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 47 | logging.debug("Unknown prompt!") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 48 | |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 49 | def pduconnect(self): |
| 50 | self.connection = self.get_connection(self.exec_string) |
| 51 | |
| 52 | def pduclose(self): |
| 53 | self.connection.close(True) |
| 54 | |
| 55 | def pdureconnect(self): |
| 56 | self.pduclose() |
| 57 | self.pduconnect() |
| 58 | |
| 59 | def get_connection(self, exec_string): |
| 60 | connection = pexpect.spawn(exec_string) |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 61 | connection.logfile = sys.stdout |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 62 | return connection |
| 63 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 64 | def is_busy(self): |
| 65 | if os.path.exists("/proc/%i" % self.connection.pid): |
| 66 | return True |
| 67 | return False |
| 68 | |
| 69 | def close(self): |
| 70 | self.driver._pdu_logout() |
| 71 | self.connection.close(True) |
| 72 | |
| 73 | def _pdu_login(self, username, password): |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame^] | 74 | logging.debug("attempting login with username %s, password %s" % (username, password)) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 75 | self.pduconnect() |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 76 | self.connection.send("\r") |
Neil Williams | 16579dc | 2014-02-13 11:20:04 +0000 | [diff] [blame^] | 77 | self.connection.expect("User Name :") |
matthew.hart@linaro.org | 93a573b | 2013-08-27 11:12:31 +0100 | [diff] [blame] | 78 | self.connection.send("apc\r") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 79 | self.connection.expect("Password :") |
matthew.hart@linaro.org | 93a573b | 2013-08-27 11:12:31 +0100 | [diff] [blame] | 80 | self.connection.send("apc\r") |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 81 | return self.connection.expect(["apc>", ">"]) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 82 | |
| 83 | |
| 84 | if __name__ == "__main__": |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 85 | #pe1 = PDUEngine("pdu15") |
| 86 | #pe1.driver.port_off(22) |
| 87 | #pe1.driver.port_on(22) |
| 88 | #pe1.close() |
| 89 | #pe2 = PDUEngine("pdu14") |
| 90 | #pe2.driver.port_off(6) |
| 91 | #pe2.driver.port_on(6) |
| 92 | #pe2.close() |
| 93 | #pe3 = PDUEngine("pdu01") |
| 94 | #pe3.driver.port_reboot(1) |
| 95 | #pe3.driver.port_off(1) |
| 96 | #pe3.driver.port_on(1) |
| 97 | #pe3.close() |
| 98 | logging.basicConfig(level=logging.DEBUG) |
| 99 | logging.getLogger().setLevel(logging.DEBUG) |
| 100 | pe4 = PDUEngine("192.168.1.153") |
| 101 | pe4.driver.port_reboot(1) |
| 102 | pe4.driver.port_reboot(2) |
| 103 | pe4.driver.port_reboot(3) |
| 104 | pe4.driver.port_reboot(4) |
| 105 | pe4.driver.port_reboot(5) |
| 106 | pe4.driver.port_reboot(6) |
| 107 | pe4.driver.port_reboot(7) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 108 | pe4.driver.port_reboot(8) |
matthew.hart@linaro.org | 753a90c | 2014-01-20 09:45:25 +0000 | [diff] [blame] | 109 | #pe4.driver.port_off(8) |
| 110 | #pe4.driver.port_on(8) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 111 | pe4.close() |