def test_ssh_client_shell(self): """Test that running command sans shell works as expected and that shell commands fail accordingly""" client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) channel, host, stdout, stderr, stdin = client.exec_command( self.fake_cmd, use_shell=False) output = list(client.read_output_buffer(stdout)) stderr = list(stderr) expected = [] exit_code = channel.recv_exit_status() self.assertEqual(expected, output, msg="Got unexpected command output - %s" % (output, )) self.assertTrue( exit_code == 127, msg="Expected cmd not found error code 127, got %s instead" % (exit_code, )) channel, host, stdout, stderr, stdin = client.exec_command( 'id', use_shell=False) output = list(client.read_output_buffer(stdout)) exit_code = channel.recv_exit_status() self.assertTrue(output, msg="Got no output from cmd executed without shell") self.assertTrue( exit_code == 0, msg="Cmd executed with shell failed with error code %s" % (exit_code, )) del client
def test_ssh_client_utf_encoding(self): """Test that unicode output works""" client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) expected = [u'é'] cmd = u"echo 'é'" channel, host, stdout, stderr, stdin = client.exec_command(cmd) output = list(client.read_output_buffer(stdout)) self.assertEqual(expected, output, msg="Got unexpected unicode output %s - expected %s" % ( output, expected,)) del client
def test_ssh_client_shell(self): """Test that running command sans shell works as expected and that shell commands fail accordingly""" client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) channel, host, stdout, stderr, stdin = client.exec_command(self.fake_cmd, use_shell=False) output = list(client.read_output_buffer(stdout)) stderr = list(stderr) expected = [] exit_code = channel.recv_exit_status() self.assertEqual(expected, output, msg = "Got unexpected command output - %s" % (output,)) self.assertTrue(exit_code == 127, msg="Expected cmd not found error code 127, got %s instead" % ( exit_code,)) channel, host, stdout, stderr, stdin = client.exec_command('id', use_shell=False) output = list(client.read_output_buffer(stdout)) exit_code = channel.recv_exit_status() self.assertTrue(output, msg="Got no output from cmd executed without shell") self.assertTrue(exit_code==0, msg="Cmd executed with shell failed with error code %s" % ( exit_code,)) del client
def test_ssh_client_utf_encoding(self): """Test that unicode output works""" client = SSHClient(self.host, port=self.listen_port, pkey=self.user_key) expected = [u'é'] cmd = u"echo 'é'" channel, host, stdout, stderr, stdin = client.exec_command(cmd) output = list(client.read_output_buffer(stdout)) self.assertEqual(expected, output, msg="Got unexpected unicode output %s - expected %s" % ( output, expected, )) del client
def test_ssh_agent_authentication(self): """Test authentication via SSH agent. Do not provide public key to use when creating SSHClient, instead override the client's agent with our own fake SSH agent, add our to key to agent and try to login to server. Key should be automatically picked up from the overriden agent""" agent = SSHAgent() agent.add_key(USER_KEY) client = SSHClient(self.host, port=self.listen_port, agent=agent) channel, host, stdout, stderr, stdin = client.exec_command(self.fake_cmd) output = list(client.read_output_buffer(stdout)) stderr = list(client.read_output_buffer(stderr)) expected = [self.fake_resp] self.assertEqual(expected, output, msg="Got unexpected command output - %s" % (output,)) del client agent._connect(None) agent._close() del agent
def test_ssh_agent_authentication(self): """Test authentication via SSH agent. Do not provide public key to use when creating SSHClient, instead override the client's agent with our own fake SSH agent, add our to key to agent and try to login to server. Key should be automatically picked up from the overriden agent""" agent = SSHAgent() agent.add_key(USER_KEY) client = SSHClient(self.host, port=self.listen_port, agent=agent) channel, host, stdout, stderr, stdin = client.exec_command( self.fake_cmd) output = list(client.read_output_buffer(stdout)) stderr = list(client.read_output_buffer(stderr)) expected = [self.fake_resp] self.assertEqual(expected, output, msg="Got unexpected command output - %s" % (output, )) del client agent._connect(None) agent._close() del agent