move pcucontrol package into pcucontrol module.
[pcucontrol.git] / pcucontrol / models / X10Toggle.py
1
2 from pcucontrol.reboot import *
3 ### rebooting x10toggle based systems addressed by port
4 # Marc E. Fiuczynski - May 31 2005
5 # tested on 4-ports models known as PSE505-FR
6 # uses ssh and password to login to an account
7 # that will cause the system to be powercycled.
8
9 TELNET_TIMEOUT = 120
10 def telnet_answer(telnet, expected, buffer):
11         global verbose
12
13         output = telnet.read_until(expected, TELNET_TIMEOUT)
14         #if verbose:
15         #       logger.debug(output)
16         if output.find(expected) == -1:
17                 raise ExceptionNotFound, "'%s' not found" % expected
18         else:
19                 telnet.write(buffer + "\r\n")
20
21 def x10toggle_reboot(ip, username, password, port, dryrun):
22         global verbose
23
24         ssh = None
25         try:
26                 ssh = pyssh.Ssh(username, ip)
27                 ssh.open()
28
29                 # Login
30                 telnet_answer(ssh, "password:", password)
31
32                 if not dryrun:
33                         # Reboot
34                         telnet_answer(ssh, "x10toggle>", "A%d" % port)
35
36                 # Close
37                 output = ssh.close()
38                 if verbose:
39                         logger.debug(output)
40                 return 0
41
42         except Exception, err:
43                 if verbose:
44                         logger.debug(err)
45                 if ssh:
46                         output = ssh.close()
47                         if verbose:
48                                 logger.debug(output)
49                 return errno.ETIMEDOUT