コード例 #1
0
def test_active_close_session(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')

    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()
    assert gateway_session.is_active()

    # open an already active session should be harmless
    gateway_session.open()
    assert gateway_session.is_active()

    remotehost_ip, remotehost_port = docker_env.get_host_ip_port('remotehost')
    remotehost_session = gateway_session.get_remote_session(
        host=tests_util.get_host_ip(),
        port=remotehost_port,
        username='******',
        password='******')
    assert remotehost_session.is_active()

    # check that gateway session is well closed
    gateway_session.close()
    assert not gateway_session.is_active()
    # remote session is also automatically closed
    assert not remotehost_session.is_active()

    # closing a closed session does nothing
    gateway_session.close()

    # running command on an inactive session will automatically open the session
    assert gateway_session.run_cmd('ls').exit_code == 0
コード例 #2
0
def test_ssh_connection_error(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')

    # open first ssh session to gateway
    gateway_session1 = SSHSession(host=gateway_ip,
                                  port=gateway_port,
                                  username='******',
                                  password='******').open()

    # modify password from session 1
    gateway_session1.run_cmd('echo "user1:newpassword" | sudo -S chpasswd')

    # try to open 2nd session
    with pytest.raises(exception.ConnectionError) as excinfo:
        SSHSession(host=gateway_ip,
                   port=gateway_port,
                   username='******',
                   password='******').open()
    assert type(excinfo.value.__cause__
                ) == paramiko.ssh_exception.AuthenticationException

    # set back correct password from session 1
    gateway_session1.run_cmd('echo "user1:password1" | sudo -S chpasswd')

    # try again to open 2nd session
    gateway_session2 = SSHSession(host=gateway_ip,
                                  port=gateway_port,
                                  username='******',
                                  password='******').open()
    assert gateway_session2.is_active()
コード例 #3
0
ファイル: test__session.py プロジェクト: t-cas/JumpSSH
def test_get_remote_session(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port()
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    remotehost_session = gateway_session.get_remote_session(
        host='remotehost', port=22, username='******', password='******')

    # run basic command on remote host
    assert remotehost_session.get_cmd_output('hostname') == 'remotehost'

    # request twice the same remote session just return the existing one
    assert gateway_session.get_remote_session(
        host='remotehost', port=22, username='******',
        password='******') == remotehost_session

    # request another remote session to another host while an existing one already exists
    remotehost2_session = remotehost_session.get_remote_session(
        host='remotehost2', port=22, username='******', password='******')
    # check that new session is active
    assert remotehost2_session.is_active()
    assert remotehost2_session.get_cmd_output('hostname') == 'remotehost2'

    # check that previous session from gateway is still active
    assert remotehost_session.is_active()
    assert remotehost_session.get_cmd_output('hostname') == 'remotehost'

    # close a remote session and check we can still request ssh session with same parameters
    remotehost2_session.close()
    assert not remotehost2_session.is_active()
    remotehost2_session = remotehost_session.get_remote_session(
        host='remotehost2', port=22, username='******', password='******')
    assert remotehost2_session.is_active()

    # close gateway session and check all children sessions are automatically closed
    gateway_session.close()
    assert not remotehost_session.is_active()
    assert not remotehost2_session.is_active()

    # get remote session from closed session should automatically open gateway session first
    # then return remote session
    remotehost_session = gateway_session.get_remote_session(
        host='remotehost', port=22, username='******', password='******')
    assert gateway_session.is_active()
    assert remotehost_session.is_active()
コード例 #4
0
ファイル: test_session.py プロジェクト: stratus-ss/JumpSSH
def test_run_cmd(docker_env, capfd):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')

    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()
    assert gateway_session.is_active()

    # basic successful command
    (exit_code, output) = gateway_session.run_cmd('hostname')
    assert exit_code == 0
    assert output.strip() == 'gateway.example.com'

    # successful list command
    gateway_session.run_cmd(['cd /etc', 'ls'])

    # wrong command
    (exit_code, output) = gateway_session.run_cmd('dummy commmand',
                                                  raise_if_error=False)
    assert exit_code == 127

    with pytest.raises(exception.RunCmdError) as excinfo:
        gateway_session.run_cmd('dummy commmand')
    assert excinfo.value.exit_code == 127
    assert excinfo.value.command == 'dummy commmand'

    # wrong command type
    with pytest.raises(TypeError):
        gateway_session.run_cmd({'key': 'value'})

    # standard output is empty by default (without continuous_output flag)
    gateway_session.run_cmd('ls -lta /')
    out, err = capfd.readouterr()
    assert len(out) == 0

    # display continuous output on stdout while command is running
    gateway_session.run_cmd('ls -lta /', continuous_output=True)
    out, err = capfd.readouterr()
    assert len(out) > 0

    # run command as user2
    assert gateway_session.run_cmd('whoami',
                                   username='******')[1].strip() == 'user2'