def pssh(s, e): r = parse_ssh_string(s)[0] assert r == e, "expected %s got %s" % (e, r)
def connect_builtin(self): #cooked vars used by connect_to username = self.config.username params = { "type": self.config.mode, "username": username, } if self.config.mode == MODE_SSH or self.config.mode == MODE_NESTED_SSH: if self.config.socket_dir: params["socket_dir"] = self.config.socket_dir params["remote_xpra"] = self.config.remote_xpra params["proxy_command"] = ["_proxy"] if self.config.port and self.config.port > 0: params["display"] = ":%s" % self.config.port params["display_as_args"] = [params["display"]] else: params["display"] = "auto" params["display_as_args"] = [] params["ssh"] = self.config.ssh params["is_putty"] = self.is_putty params["is_paramiko"] = self.is_paramiko password = self.config.password host = self.config.host upos = host.find("@") if upos >= 0: #found at sign: username@host username = host[:upos] host = host[upos + 1:] ppos = username.find(":") if ppos >= 0: #found separator: username:password@host password = username[ppos + 1:] username = username[:ppos] if self.config.ssh_port and self.config.ssh_port != 22: params["ssh-port"] = self.config.ssh_port ssh_cmd = parse_ssh_string(self.config.ssh) ssh_cmd_0 = ssh_cmd[0].strip().lower() self.is_putty = ssh_cmd_0.endswith("plink") or ssh_cmd_0.endswith( "plink.exe") self.is_paramiko = ssh_cmd_0 == "paramiko" full_ssh = ssh_cmd[:] full_ssh += add_ssh_args(username, password, host, self.config.ssh_port, None, self.is_putty, self.is_paramiko) if username: params["username"] = username if self.nostrict_host_check.get_active(): full_ssh += ["-o", "StrictHostKeyChecking=no"] if params["type"] == MODE_NESTED_SSH: params["type"] = "ssh" params["proxy_host"] = self.config.proxy_host params["proxy_port"] = self.config.proxy_port params["proxy_username"] = self.config.proxy_username params["proxy_password"] = self.config.proxy_password full_ssh += add_ssh_proxy_args(self.config.proxy_username, self.config.proxy_password, self.config.proxy_host, self.config.proxy_port, self.config.proxy_key, ssh_cmd, self.is_putty, self.is_paramiko) params["host"] = host params["local"] = is_local(self.config.host) params["full_ssh"] = full_ssh params["password"] = password params["display_name"] = "ssh:%s:%s" % (self.config.host, self.config.port) elif self.config.mode == "unix-domain": params["display"] = ":%s" % self.config.port params["display_name"] = "unix-domain:%s" % self.config.port else: assert self.config.mode in ( MODE_TCP, MODE_SSL, MODE_WS, MODE_WSS), "invalid / unsupported mode %s" % self.config.mode params["host"] = self.config.host params["local"] = is_local(self.config.host) params["port"] = int(self.config.port) params["display_name"] = "%s:%s:%s" % ( self.config.mode, self.config.host, self.config.port) if self.config.mode in ( MODE_SSL, MODE_WSS) and self.nostrict_host_check.get_active(): params["strict-host-check"] = False #print("connect_to(%s)" % params) #UGLY warning: the username may have been updated during display parsing, #or the config file may contain a username which is different from the default one #which is used for initializing the client during init, #so update the client now: configure_env(self.config.env) configure_logging(self.config, "attach") configure_network(self.config) self.start_client(params)
def test_ssh_parsing(self): assert parse_ssh_string("auto")[0] in ("paramiko", "ssh") assert parse_ssh_string("ssh") == ["ssh"] assert parse_ssh_string("ssh -v") == ["ssh", "-v"] with nomodule_context("paramiko"): add_debug_category("ssh") def pssh(s, e): r = parse_ssh_string(s)[0] assert r == e, "expected %s got %s" % (e, r) if WIN32: pssh("auto", "plink.exe") else: pssh("auto", "ssh") remove_debug_category("ssh") #args: def targs(e, *args, **kwargs): r = add_ssh_args(*args, **kwargs) assert r == e, "expected %s but got %s" % (e, r) targs([], None, None, None, None, None, is_paramiko=True) targs( ["-pw", "password", "-l", "username", "-P", "2222", "-T", "host"], "username", "password", "host", 2222, None, is_putty=True) if not WIN32: keyfile = os.path.expanduser("~/key") targs( ["-l", "username", "-p", "2222", "-T", "host", "-i", keyfile], "username", "password", "host", 2222, keyfile) #proxy: def pargs(e, n, *args, **kwargs): r = add_ssh_proxy_args(*args, **kwargs)[:n] assert r == e, "expected %s but got %s" % (e, r) pargs(["-o"], 1, "username", "password", "host", 222, None, ["ssh"]) pargs(["-proxycmd"], 1, "username", "password", "host", 222, None, ["putty.exe"], is_putty=True) #proxy attributes: assert parse_proxy_attributes("somedisplay") == ("somedisplay", {}) attr = parse_proxy_attributes("10?proxy=username:password@host:222")[1] assert attr == { "proxy_host": "host", "proxy_port": 222, "proxy_username": "******", "proxy_password": "******" } def f(s): v = parse_proxy_attributes(s) assert v[1] == {}, "parse_proxy_attributes(%s) should fail" % s f("somedisplay?proxy=") f("somedisplay?proxy=:22") f("somedisplay?proxy=:@host:22") f("somedisplay?proxy=:password@host:22")
def parse_ssh(self): ssh_cmd = parse_ssh_string(self.config.ssh)[0].strip().lower() self.is_putty = ssh_cmd.endswith("plink") or ssh_cmd.endswith( "plink.exe") self.is_paramiko = ssh_cmd.startswith("paramiko")