def settings(): """Gives the config to the tests if the host resolves, or skips.""" conf = config() if not can_resolve(conf["host"]): pytest.skip("cannot resolve {0}".format(conf["host"])) return conf
def connect(self, target, username, password, port): """Connects to a server, maybe from another server. Args:: target: the hostname, as a string username: the user we are connecting as password: list or string plain text password(s) to try port: ssh port number, as integer Returns: a pexpect object that can be passed back here or to send_commands() """ if not can_resolve(target): return (None, -3) ssh_cmd = self._build_ssh_command(target, username, port) if not self.sshc: try: sshr = pexpect.spawn(ssh_cmd, timeout=self.options["timeout"]) if self.options["debug"]: sshr.logfile_read = FakeStdOut login_response = sshr.expect( self.options["passwd_prompts"] + self.options["shell_prompts"] + self.options["extra_prompts"], self.options["timeout"], ) if self.options["jump_host"]: self.sshc = sshr return self._multipass(sshr, password, login_response) except (pexpect.TIMEOUT, pexpect.EOF): if sshr.isalive(): # logged in with no passwd and an unknown prompt return self._try_for_unmatched_prompt( sshr, sshr.before, ssh_cmd, _from_login=True, ) else: return (None, -7) else: self.sshc.sendline(ssh_cmd) try: login_response = self.sshc.expect( self.options["passwd_prompts"] + self.options["shell_prompts"] + self.options["extra_prompts"], self.options["timeout"], ) except (pexpect.TIMEOUT, pexpect.EOF): # XXX: possible to use a jumpbox and login without a passwd # and the shell prompt is unknown... can't use isalive tho # so, this results in an error for now. workaround is to # provide the expected after-jumpbox expected shell prompt self.send_interrupt(self.sshc) return (None, -1) if self.sshc.before.find(six.b("Permission denied")) != -1: self.send_interrupt(self.sshc) return (None, -4) for net_err in ("Network is unreachable", "Connection refused"): if self.sshc.before.find(six.b(net_err)) != -1: self.send_interrupt(self.sshc) return (None, -7) return self._multipass(self.sshc, password, login_response)
def connect(self, target, username, password, port): """Connects to a server, maybe from another server. Args:: target: the hostname, as a string username: the user we are connecting as password: plain text password to pass port: ssh port number, as integer Returns: a pexpect object that can be passed back here or to send_commands() """ if not can_resolve(target): return (None, -3) if not self.sshc: try: if self.options["ssh_key"] and \ os.path.isfile(self.options["ssh_key"]): sshr = pexpect.spawn( "ssh -p {portnumber} -ti {key} {user}@{host}".format( portnumber=port, key=self.options["ssh_key"], user=username, host=target, ) ) else: sshr = pexpect.spawn( "ssh -p {portnumber} -t {user}@{host}".format( portnumber=port, user=username, host=target, ) ) login_response = sshr.expect( self.options["passwd_prompts"] + self.options["shell_prompts"] + self.options["extra_prompts"], self.options["timeout"], ) if self.options["jump_host"]: self.sshc = sshr return self.login(self.sshc, password, login_response) else: return self.login(sshr, password, login_response) except (pexpect.TIMEOUT, pexpect.EOF): return (None, -1) else: if self.options["ssh_key"]: self.sshc.sendline("ssh -ti {key} {user}@{host}".format( key=self.options["ssh_key"], user=username, host=target, )) else: self.sshc.sendline("ssh -t {user}@{host}".format( user=username, host=target, )) try: login_response = self.sshc.expect( self.options["passwd_prompts"] + self.options["shell_prompts"] + self.options["extra_prompts"], self.options["timeout"], ) except (pexpect.TIMEOUT, pexpect.EOF): self.send_interrupt(self.sshc) return (None, -1) if self.sshc.before.find("Permission denied") != -1: self.send_interrupt(self.sshc) return (None, -4) return self.login(self.sshc, password, login_response)
def test_can_resolve(): """Basic test case for the can_resolve function.""" assert can_resolve("google.com") assert not can_resolve("googly.boogly.doodley-do.1234abcd")
def connect(self, target, username, password, port): """Connects to a server, maybe from another server. Args:: target: the hostname, as a string username: the user we are connecting as password: list or string plain text password(s) to try port: ssh port number, as integer Returns: a pexpect object that can be passed back here or to send_commands() """ if self.options["ssh"] == "ssh" and not can_resolve(target): return (None, -3) ssh_cmd = self._build_ssh_command(target, username, port) if not self.sshc: try: sshr = pexpect.spawn(ssh_cmd, timeout=self.options["timeout"]) if self.options["debug"]: sshr.logfile_read = FakeStdOut login_response = sshr.expect( self.options["passwd_prompts"] + self.options["shell_prompts"] + self.options["extra_prompts"], self.options["timeout"], ) if self.options["jump_host"]: self.sshc = sshr return self._multipass(sshr, password, login_response) except (pexpect.TIMEOUT, pexpect.EOF): if sshr.isalive(): # logged in with no passwd and an unknown prompt return self._try_for_unmatched_prompt( sshr, sshr.before, ssh_cmd, _from_login=True, ) else: return (None, -7) else: self.sshc.sendline(ssh_cmd) try: login_response = self.sshc.expect( self.options["passwd_prompts"] + self.options["shell_prompts"] + self.options["extra_prompts"], self.options["timeout"], ) except (pexpect.TIMEOUT, pexpect.EOF): # XXX: possible to use a jumpbox and login without a passwd # and the shell prompt is unknown... can't use isalive tho # so, this results in an error for now. workaround is to # provide the expected after-jumpbox expected shell prompt self.send_interrupt(self.sshc) return (None, -1) if self.sshc.before.find(six.b("Permission denied")) != -1: self.send_interrupt(self.sshc) return (None, -4) for net_err in ("Network is unreachable", "Connection refused"): if self.sshc.before.find(six.b(net_err)) != -1: self.send_interrupt(self.sshc) return (None, -7) return self._multipass(self.sshc, password, login_response)