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