Skip to content

Instantly share code, notes, and snippets.

@joshmarshall
Created June 18, 2011 18:37
Show Gist options
  • Save joshmarshall/1033384 to your computer and use it in GitHub Desktop.
Save joshmarshall/1033384 to your computer and use it in GitHub Desktop.
A script for starting an x2x+SSH session
#!/usr/bin/env python
"""
AUTHOR: Josh Marshall
ACKNOWLEDGMENTS: Corey(?) http://www.ctrlv.ca/2010/04/163/
REQUIREMENTS: Python 2.6+, x2x, openssh-server
This is just a simple script to start an x2x session over SSH
on a remote machine. It takes a few parameters (like what side of
the screen do you want to use to "cross over" to the other machine,
what SSH port / identify file to use, etc.
<TLDR>Magic mouse moving script!</TLDR>
It's only been tested on Ubuntu 11.04 -> 11.04, so I've got that
0.0001% of the population covered.
INSTALLATION:
Just make sure that you have x2x on both machines, and openssh-server
on the remote machine. You SHOULD be able to just enter your password
if you haven't hooked up your .pub file (naughty), but let me know
if it's broken on your system.
RUNNING:
Just run the following command:
python mouseporter.py 192.168.1.1
// or
python mouseporter.py [email protected]
...and it should work with basic defaults. If you need to set custom
flags, run the following:
python mouseporter.py -h
...to get a list of available options.
"""
import subprocess
import sys
import optparse
import logging
# All the SSH / x2x options that we support right now...
OPTIONS = optparse.OptionParser("python %prog [user@]host OPTIONS")
OPTIONS.add_option("-p", "--port", help="the port of the remote machine",
dest="port")
OPTIONS.add_option("-d", "--direction", help="the side of the newscreen",
dest="direction", default="east")
OPTIONS.add_option("-c", "--screen", help="which remote screen to use",
dest="screen", default="0")
OPTIONS.add_option("-i", "--identity", help="the identity file",
dest="identity")
OPTIONS.add_option("-s", "--ssh", help="the location of the ssh binary",
dest="ssh", default="ssh")
# The valid screen directions
DIRECTIONS = ["north", "south", "east", "west"]
def error(message, *args):
""" Logs an error message, the usage, then exits. """
logging.error(message, *args)
OPTIONS.print_usage(sys.stderr)
sys.exit(1)
def main():
""" Starts the session, monitors for keyboard interrupt """
options, args = OPTIONS.parse_args()
if not len(args) == 1:
error("The host argument is missing.")
if options.direction not in DIRECTIONS:
error("The direction must one of %s", ", ".join(DIRECTIONS))
try:
int(options.screen)
except ValueError:
error("The screen must be an integer (usually 0)")
host = args[0]
command = [options.ssh, "-X"]
if options.port:
command += ["-p", options.port]
if options.identity:
command += ["-i", options.identity]
command.append(host)
command += ["x2x", "-%s" % options.direction,
"-to", ":%s" % options.screen]
proc = subprocess.Popen(command)
print "Session started... (type Ctrl-C to quit)"
try:
proc.wait()
except KeyboardInterrupt:
logging.info("Keyboard interrupt detected.")
proc.kill()
print "Session ended."
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment