def test_ssh_returns_proper_command_string(buffer_connection): ssh_cmd = Ssh(buffer_connection, login="******", password="******", host="host.domain.net", expected_prompt="host:.*#") assert "TERM=xterm-mono ssh -l user host.domain.net" == ssh_cmd.command_string
def test_calling_ssh_returns_result_parsed_from_command_output(buffer_connection, command_output_and_expected_result): command_output = command_output_and_expected_result buffer_connection.remote_inject_response([command_output]) ssh_cmd = Ssh(connection=buffer_connection.moler_connection, login="******", password="******", host="host.domain.net", expected_prompt="host:.*#") assert "TERM=xterm-mono ssh -l user host.domain.net" == ssh_cmd.command_string assert ssh_cmd() is not None
def test_ssh_failed_known_hosts(buffer_connection, command_output_failed_known_hosts): command_output = command_output_failed_known_hosts buffer_connection.remote_inject_response([command_output]) ssh_cmd = Ssh(connection=buffer_connection.moler_connection, login="******", password="******", prompt="client:~ #", host="host.domain.net", expected_prompt="host:.*#", known_hosts_on_failure='badvalue') assert "TERM=xterm-mono ssh -l user host.domain.net" == ssh_cmd.command_string with pytest.raises(CommandFailure): ssh_cmd()
def test_ssh_failed_permission_denied(buffer_connection, command_output_permission_denied): command_output = command_output_permission_denied buffer_connection.remote_inject_response([command_output]) ssh_cmd = Ssh(connection=buffer_connection.moler_connection, login="******", password="******", host="host.domain.net", expected_prompt="host:.*#") assert "TERM=xterm-mono ssh -l user host.domain.net" == ssh_cmd.command_string with pytest.raises(CommandFailure): ssh_cmd()
def test_ssh_username_and_login(buffer_connection): with pytest.raises(CommandFailure) as ex: Ssh(connection=buffer_connection.moler_connection, login="******", password="******", port=1500, host="host.domain.net", expected_prompt=r"host:.*#", prompt=r"user@client.*>", username="******") assert "not both" in str(ex) assert "Ssh" in str(ex)
def test_ssh_failed_host_key_verification( buffer_connection, command_output_failed_host_key_verification): command_output = command_output_failed_host_key_verification buffer_connection.remote_inject_response([command_output]) ssh_cmd = Ssh(connection=buffer_connection.moler_connection, login="******", password="******", host="host.domain.net", expected_prompt="server:.*#", prompt="host:~ #", options=None) assert "TERM=xterm-mono ssh -l user host.domain.net" == ssh_cmd.command_string with pytest.raises(CommandFailure): ssh_cmd()
def test_ssh_failed_with_multiple_passwords(buffer_connection, command_output_2_passwords): command_output = command_output_2_passwords buffer_connection.remote_inject_response([command_output]) ssh_cmd = Ssh(connection=buffer_connection.moler_connection, login="******", password="******", host="host.domain.net", expected_prompt="host:.*#", prompt="starthost:~ >", repeat_password=False, options=None) assert "TERM=xterm-mono ssh -l user host.domain.net" == ssh_cmd.command_string with pytest.raises(CommandFailure): ssh_cmd()
def test_ssh_failed_authentication_failed( buffer_connection, command_output_authentication_failed): command_output = command_output_authentication_failed buffer_connection.remote_inject_response([command_output]) ssh_cmd = Ssh(connection=buffer_connection.moler_connection, login="******", password="******", prompt="client:.*$", host="host.domain.net", expected_prompt="host:.*#") expected_cmd_str = "TERM=xterm-mono ssh -l user -o ServerAliveInterval=7 -o ServerAliveCountMax=2 host.domain.net" assert expected_cmd_str == ssh_cmd.command_string with pytest.raises(CommandFailure) as err: ssh_cmd() assert "Authentication fail" in str(err.value)
def test_ssh_prompt_in_the_same_line(buffer_connection, command_output_prompt_in_the_same_line): command_output = command_output_prompt_in_the_same_line buffer_connection.remote_inject_response([command_output]) ssh_cmd = Ssh(connection=buffer_connection.moler_connection, login="******", password="******", set_timeout=None, set_prompt=None, host="host.domain.net", prompt="client.*>", expected_prompt="^host.*#$") assert ssh_cmd.enter_on_prompt_without_anchors is True ssh_cmd(timeout=1) assert ssh_cmd.enter_on_prompt_without_anchors is False
def test_ssh_timeout_with_wrong_change_prompt_after_login( buffer_connection, command_output_change_prompt): command_output = command_output_change_prompt buffer_connection.remote_inject_response([command_output]) ssh_cmd = Ssh(connection=buffer_connection.moler_connection, login="******", password="******", set_prompt=r'export PS1="\\u$"', host="host.domain.net", prompt="client.*>", expected_prompt=r"user\$", prompt_after_login=r"wronghost.*#", options=None) with pytest.raises(CommandTimeout): ssh_cmd(timeout=0.2)
def test_ssh_change_rm_command(buffer_connection, command_output_keygen): command_output = command_output_keygen buffer_connection.remote_inject_response([command_output]) ssh_cmd = Ssh(connection=buffer_connection.moler_connection, login="******", password="******", set_timeout=None, host="10.0.1.67", prompt="client.*>", expected_prompt="host.*#") assert ssh_cmd._permission_denied_key_pass_keyboard_cmd == 'ssh-keygen -f "~/.ssh/known_hosts" -R "10.0.1.67"' ssh_cmd(timeout=1) ssh_cmd.break_cmd(silent=True) ssh_cmd.break_cmd(silent=False) assert ssh_cmd._permission_denied_key_pass_keyboard_cmd == 'ssh-keygen -f "/home/user/.ssh/known_hosts" -R "10.0.1.67"'