move pcucontrol package into pcucontrol module.
[pcucontrol.git] / pcucontrol / models / X10Toggle.py
diff --git a/pcucontrol/models/X10Toggle.py b/pcucontrol/models/X10Toggle.py
new file mode 100644 (file)
index 0000000..20d7cef
--- /dev/null
@@ -0,0 +1,49 @@
+
+from pcucontrol.reboot import *
+### rebooting x10toggle based systems addressed by port
+# Marc E. Fiuczynski - May 31 2005
+# tested on 4-ports models known as PSE505-FR
+# uses ssh and password to login to an account
+# that will cause the system to be powercycled.
+
+TELNET_TIMEOUT = 120
+def telnet_answer(telnet, expected, buffer):
+       global verbose
+
+       output = telnet.read_until(expected, TELNET_TIMEOUT)
+       #if verbose:
+       #       logger.debug(output)
+       if output.find(expected) == -1:
+               raise ExceptionNotFound, "'%s' not found" % expected
+       else:
+               telnet.write(buffer + "\r\n")
+
+def x10toggle_reboot(ip, username, password, port, dryrun):
+       global verbose
+
+       ssh = None
+       try:
+               ssh = pyssh.Ssh(username, ip)
+               ssh.open()
+
+               # Login
+               telnet_answer(ssh, "password:", password)
+
+               if not dryrun:
+                       # Reboot
+                       telnet_answer(ssh, "x10toggle>", "A%d" % port)
+
+               # Close
+               output = ssh.close()
+               if verbose:
+                       logger.debug(output)
+               return 0
+
+       except Exception, err:
+               if verbose:
+                       logger.debug(err)
+               if ssh:
+                       output = ssh.close()
+                       if verbose:
+                               logger.debug(output)
+               return errno.ETIMEDOUT