#!/usr/bin/env python # Simple client for NX. # Adam Sampson import os home = os.getenv("HOME") version = "0.1" host = "localhost" port = 22 username = "azz" password = "insertpasswordhere" root = home + "/.nx" nxssh = "nxbuild/nxssh/nxssh" nxproxy = "nxbuild/nxproxy/nxproxy" key = "/usr/NX/share/client.id_dsa.key" class NXError(Exception): pass def start_session(): socksport = 32222 cmd = "%s -nx -i %s -p %d nx@%s -D %d -x -2 2>&1" % (nxssh, key, port, host, socksport) print "--- " + cmd (ci, co) = os.popen2(cmd) def send(s): print ">>> " + s ci.write(s + "\n") ci.flush() def read(): while 1: l = co.readline() if l == "": return (-1, None) l = l[:-1] print "<<< " + l if l.startswith("NX> "): return (int(l[4:7]), l[8:]) send("hello NXCLIENT - Version 1.3.2") while 1: (c, s) = read() assert c < 400 if c == 134: break # These must be all caps or all lowercase for FreeNX. send("SET SHELL_MODE SHELL") (c, s) = read() assert c == 105 send("SET AUTH_MODE PASSWORD") (c, s) = read() assert c == 105 send("login") (c, s) = read() assert c == 105 send(username) (c, s) = read() assert c == 101 send(password) (c, s) = read() assert c == 102 (c, s) = read() assert c == 103 f = os.popen("xauth -f %s/.Xauthority nextract - %s" % (home, os.getenv("DISPLAY"))) cookie = f.readline().strip().split(" ")[8] f.close() session = "testdrive" args = ( ("session", session), ("type", "unix-gnome"), ("cache", "8M"), ("images", "32M"), ("cookie", cookie), ("link", "adsl"), ("encryption", "1"), ("backingstore", "never"), # or ("when_requested" ("geometry", "800x600+112+84"), ("keyboard", "gb"), ("kbtype", "pc102/gb"), ("media", "0"), ("agent_server", ""), ("agent_user", ""), ("agent_password", ""), ) send("startsession " + " ".join(map(lambda (k, v): "--" + k + '="' + v + '"', args))) (c, s) = read() assert c == 105 info = {} while 1: (c, s) = read() if int(c / 100) == 7: (k, v) = s.split(": ", 1) info[k] = v if k == "Session status": break elif int(c / 100) == 10: # Only generated by FreeNX? pass sessiondir = root + "/S-" + info["Session id"] os.mkdir(sessiondir) optionsfile = sessiondir + "/options" f = open(optionsfile, "w") options = ( ("cookie", info["Proxy cookie"]), ("root", root), ("session", session), ("id", info["Session id"]), ("connect", host + ":" + info["Session display"]), ) f.write(",".join(map(lambda (k, v): k + "=" + v, options)) + "\r\n") f.close() cmd = (nxproxy, "-P", str(socksport), "-S", "options=%s:%s" % (optionsfile, info["Session display"])) print "---", cmd nxproxypid = os.spawnvp(os.P_NOWAIT, cmd[0], cmd) while 1: (c, s) = read() if c == -1: break if __name__ == "__main__": start_session()