blob: d3dca6f668f7df47538a8a1bab1af3066163700a [file] [log] [blame]
Matt Hart2af71142014-07-22 10:40:41 +01001#! /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
21import logging
22import pexpect
23from driver import PDUDriver
24import sys
25
26class APCBase(PDUDriver):
27 connection = None
28
29 def __init__(self, pdu_hostname, pdu_telnetport=23):
30 self.exec_string = "/usr/bin/telnet %s %d" % (pdu_hostname, pdu_telnetport)
31 self.get_connection()
32 super(APCBase, self).__init__(pdu_hostname)
33
34 #def port_on(self, port_number):
35 # self.port_interaction("on", port_number)
36
37 #def port_off(self, port_number):
38 # self.port_interaction("off", port_number)
39
40 def port_interaction(self, command, port_number):
41 logging.debug("Running port_interaction from APCBase")
42 self._port_interaction(command, port_number)
43 self._cleanup()
44
45 def get_connection(self):
46 logging.debug("Connecting to APC PDU with: %s" % self.exec_string)
47 self.connection = pexpect.spawn(self.exec_string, logfile=sys.stdout)
48 self._pdu_login("apc","apc")
49
50 def _cleanup(self):
51 self._pdu_logout()
52 logging.debug("Closing connection: %s" % self.connection)
53 self.connection.close(True)
54 del(self)
55
56 def _pdu_login(self, username, password):
57 logging.debug("attempting login with username %s, password %s" % (username, password))
58 self.connection.send("\r")
59 self.connection.expect("User Name :")
60 self.connection.send("%s\r" % username)
61 self.connection.expect("Password :")
62 self.connection.send("%s\r" % password)