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