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 |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 24 | |
Matt Hart | d73bba2 | 2013-08-21 13:46:03 +0100 | [diff] [blame] | 25 | from apcdrivers import apc8959 |
| 26 | from apcdrivers import apc7952 |
Matt Hart | 96ca866 | 2013-08-21 13:37:35 +0100 | [diff] [blame] | 27 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 28 | |
| 29 | class PDUEngine(): |
| 30 | connection = None |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 31 | prompt = 0 |
| 32 | driver = None |
| 33 | |
| 34 | def __init__(self, pdu_hostname, pdu_telnetport = 23): |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 35 | self.exec_string = "/usr/bin/telnet %s %d" % (pdu_hostname, pdu_telnetport) |
| 36 | logging.debug("Created new PDUEngine: %s" % self.exec_string) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 37 | #self.connection.logfile_read = sys.stdout |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 38 | prompt = self._pdu_login("apc","apc") |
| 39 | if prompt == 0: |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 40 | logging.debug("Found a v5 prompt") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 41 | self.driver = apc8959(self.connection) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 42 | elif prompt == 1: |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 43 | logging.debug("Found a v3 prompt") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 44 | self.driver = apc7952(self.connection) |
| 45 | else: |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 46 | logging.debug("Unknown prompt!") |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 47 | |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 48 | def pduconnect(self): |
| 49 | self.connection = self.get_connection(self.exec_string) |
| 50 | |
| 51 | def pduclose(self): |
| 52 | self.connection.close(True) |
| 53 | |
| 54 | def pdureconnect(self): |
| 55 | self.pduclose() |
| 56 | self.pduconnect() |
| 57 | |
| 58 | def get_connection(self, exec_string): |
| 59 | connection = pexpect.spawn(exec_string) |
| 60 | return connection |
| 61 | |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 62 | def is_busy(self): |
| 63 | if os.path.exists("/proc/%i" % self.connection.pid): |
| 64 | return True |
| 65 | return False |
| 66 | |
| 67 | def close(self): |
| 68 | self.driver._pdu_logout() |
| 69 | self.connection.close(True) |
| 70 | |
| 71 | def _pdu_login(self, username, password): |
Matt Hart | 63ed911 | 2013-08-20 13:31:50 +0100 | [diff] [blame] | 72 | logging.debug("attempting login with username %s, password %s" % (username,password)) |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 73 | self.pduconnect() |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 74 | self.connection.send("\r") |
| 75 | self.connection.expect ("User Name :") |
| 76 | self.connection.send("apc\r") |
| 77 | self.connection.expect("Password :") |
| 78 | self.connection.send("apc\r") |
Matt Hart | 40d6c31 | 2013-08-20 14:47:31 +0100 | [diff] [blame] | 79 | return self.connection.expect(["apc>", ">"]) |
Matt Hart | 1306d82 | 2013-08-09 12:08:22 +0100 | [diff] [blame] | 80 | |
| 81 | |
| 82 | if __name__ == "__main__": |
| 83 | pe1 = PDUEngine("pdu15") |
| 84 | pe1.driver.port_off(22) |
| 85 | pe1.driver.port_on(22) |
| 86 | pe1.close() |
| 87 | pe2 = PDUEngine("pdu14") |
| 88 | pe2.driver.port_off(6) |
| 89 | pe2.driver.port_on(6) |
| 90 | pe2.close() |
| 91 | pe3 = PDUEngine("pdu01") |
| 92 | pe3.driver.port_reboot(1) |
| 93 | pe3.driver.port_off(1) |
| 94 | pe3.driver.port_on(1) |
| 95 | pe3.close() |
| 96 | pe4 = PDUEngine("pdu02") |
| 97 | pe4.driver.port_reboot(8) |
| 98 | pe4.driver.port_off(8) |
| 99 | pe4.driver.port_on(8) |
| 100 | pe4.close() |
| 101 | |