• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import telnetlib, threading, time
6
7# Controls Server Technology CW-16V1-C20M switched CDUs over Telnet
8# Opens a new connection for every command to
9# avoid threading and address space conflicts
10
11class PowerStrip():
12    def __init__(self, host, user='admn', password='admn'):
13        self.host = host
14        self.user = user
15        self.password = password
16
17    def reboot(self, outlet, delay=0):
18        self.command('reboot', outlet, delay)
19
20    def off(self, outlet, delay=0):
21        self.command('off', outlet, delay)
22
23    def on(self, outlet, delay=0):
24        self.command('on', outlet, delay)
25
26    def command(self, command, outlet=1, delay=0):
27        if delay == 0:
28            self._do_command(command, outlet)
29        else:
30            threading.Timer(delay, self._do_command, (command, outlet)).start()
31
32    def _do_command(self, command, outlet=1):
33        tn = telnetlib.Telnet(self.host)
34        tn.read_until('Username: ')
35        tn.write(self.user + '\n')
36        tn.read_until('Password: ')
37        tn.write(self.password + '\n')
38        tn.read_until('Switched CDU: ')
39        tn.write('%s .a%d\n' % (command, outlet))
40        tn.read_some()
41        tn.close()
42