From d3f070850f3e5e4c3882b923265b43f72e553061 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 16 Mar 2013 13:17:59 +0100 Subject: [PATCH] Add script to establish SSH daemon tunnels --- command_daemon/start.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 command_daemon/start.py diff --git a/command_daemon/start.py b/command_daemon/start.py new file mode 100644 index 0000000..79e8d16 --- /dev/null +++ b/command_daemon/start.py @@ -0,0 +1,23 @@ +import sys, subprocess + +stfu = open("/dev/null", "w") + +def run_command(args): + pr = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + data = pr.communicate() + pr.wait() + return pr.returncode + +host = sys.argv[1] +user = sys.argv[2] +port = int(sys.argv[3]) +keyfile = sys.argv[4] +session_key = sys.argv[5] + +if run_command(["ssh", "%s@%s" % (user, host), "-i", keyfile, "cd /etc/cvm/command_daemon; echo '%s' > session_key && ./command_daemon" % session_key]) == 0: + if run_command(["autossh", "-f", "-i", keyfile, "-M", str(port + 1), "%s@%s" % (user, host), "-L", "%s:localhost:3434" % port, "-N"]) == 0: + exit(0) + else: + sys.stderr.write("Failed to establish tunnel.\n") +else: + sys.stderr.write("Failed to start daemon.\n")