Example #1
0
def test_multihop_tunnel_tunnel_by_name():
    user1 = properties.UserInfo(username='******',
                                password='******',
                                expected_prompt=r'{username}@.+?:~\$ ')
    tunnel = dict(hops=[{
        'host': '127.0.0.1',
        'user': user1,
        'port': 922,
        'password': '******'
    }, {
        'host': '127.0.0.1',
        'user': user1,
        'proto': 'telnet'
    }, {
        'host': '127.0.0.1',
        'user': user1
    }])

    d = Device('localhost',
               connections=dict(ssh={}),
               users=dict(learner=user1),
               tunnels=dict(t1=tunnel))
    with d.conn.open(tunnel='t1'):
        try:
            if not d.check_output('whoami') == d.users.default.username:
                raise Exception
            else:
                assert True
        except Exception:
            import pprint
            pprint.pprint(d.conn.get_all_conversations_flat())
            raise
Example #2
0
def test_cmd_connection_no_tunnel():
    user1 = properties.UserInfo(username='******',
                                password='******',
                                expected_prompt=r'{username}@.+?:~\$ ')
    d = Device('localhost',
               connections=dict(
                   t={
                       'proto': 'command',
                       'cmd': 'telnet 127.0.0.1',
                       'user': user1,
                       'can_change_prompt': False
                   }),
               users=dict(learner=user1))
    with pytest.raises(ConnectionError):
        with d.conn.open():
            try:
                if not d.check_output('whoami') == d.users.default.username:
                    raise Exception
                else:
                    assert True
            except Exception:
                import pprint
                pprint.pprint(d.conn.get_all_conversations_flat())

                raise
Example #3
0
def test_saving_restoring_connections_to_db():
    update_db_config()

    DEF_CONN_DICT = dict(proto='ssh', port=922)
    user1 = properties.UserInfo(password='******',
                                expected_prompt=r'{username}@.+?:~\$ ')
    d = Device('localhost',
               connections=dict(ssh=DEF_CONN_DICT),
               users=dict(learner=user1))
    d.os.can_change_prompt = False
    with d.conn.open():
        assert re.search(user1.expected_prompt.format(username='******'),
                         d.conn.prompt.replace('\\', '')) is not None

    try:
        d.save()
    except Exception:
        from fdutils.db import sasessioninit
        #d.dbsession.rollback()
        raise

    d2 = Device.get_by_hostname('localhost')
    with d2.conn.open():
        assert re.search(user1.expected_prompt.format(username='******'),
                         d2.conn.prompt_found.replace('\\', '')) is not None
Example #4
0
def test_multihop_mix_proto_tunnel_as_list_of_hops():
    user1 = properties.UserInfo(username='******',
                                password='******',
                                expected_prompt=r'{username}@.+?:~\$ ')
    hops = [{
        'host': '127.0.0.1',
        'user': user1,
        'port': 922,
        'password': '******'
    }, {
        'host': '127.0.0.1',
        'user': user1,
        'proto': 'telnet'
    }, {
        'host': '127.0.0.1',
        'user': user1
    }]

    d = Device('localhost',
               connections=dict(ssh={'tunnel': dict(hops=hops)}),
               users=dict(learner=user1))
    import time
    t0 = time.time()

    with d.conn.open():
        print(time.time() - t0)
        try:
            if not d.check_output('whoami') == d.users.default.username:
                raise Exception
            else:
                assert True
        except Exception:
            import pprint
            pprint.pprint(d.conn.get_all_conversations_flat())
            raise
Example #5
0
def test_default_ip_persistent():
    update_db_config()
    d = Device('localhost', default_ip_address='127.0.0.1')
    assert d.default_ip_address == '127.0.0.1'

    d.save()

    d2 = Device.get_by_hostname('localhost')
    assert d2.default_ip_address == '127.0.0.1'
Example #6
0
def test_base_other_os_persistence():
    update_db_config()

    d = Device('localhost', os_name='Windows')
    assert d.os.__class__ == WindowsOS
    d.save()

    d2 = Device.get_by_hostname('localhost')
    assert d2.os.__class__ == WindowsOS
Example #7
0
def test_device_persintence():
    update_db_config()
    cur_path = os.path.realpath('.')
    d = Device('localhost', storage_path=cur_path)
    assert d.folder == os.path.join(cur_path, 'localhost')
    d.save()

    d2 = Device.get_by_hostname('localhost')
    assert d2.folder == os.path.join(cur_path, 'localhost')
Example #8
0
def test_saving_restoring_connections_multihop_to_db():
    update_db_config()

    user1 = properties.UserInfo(username='******',
                                password='******',
                                expected_prompt=r'{username}@.+?:~\$ ')
    jump_boxes = [{
        'host': '127.0.0.1',
        'user': user1,
        'port': 922
    }, {
        'host': '127.0.0.1',
        'user': user1
    }, {
        'host': '127.0.0.1',
        'user': user1
    }]

    d = Device('localhost',
               connections=dict(ssh={'tunnel': dict(hops=jump_boxes)}),
               users=dict(learner=user1),
               encrypt_passwords=True)

    t0 = time.time()
    d.os.can_change_prompt = False
    with d.conn.open():
        print(time.time() - t0)
        assert re.search(user1.expected_prompt.format(username='******'),
                         d.conn.prompt.replace('\\', '')) is not None

    try:
        d.save()
    except Exception:
        from fdutils.db import sasessioninit
        #d.dbsession.rollback()
        raise

    t0 = time.time()
    d2 = Device.get_by_hostname('localhost')
    with d2.conn.open():
        print(time.time() - t0)
        assert re.search(user1.expected_prompt.format(username='******'),
                         d2.conn.prompt_found.replace('\\', '')) is not None
    t0 = time.time()
    with d2.conn.open():
        print(time.time() - t0)
        assert re.search(user1.expected_prompt.format(username='******'),
                         d2.conn.prompt_found.replace('\\', '')) is not None
Example #9
0
def test_multihop_hostname_interface():
    user1 = properties.UserInfo(username='******',
                                password='******',
                                expected_prompt=r'{username}@.+?:~\$ ')
    hops = [{
        'host': '127.0.0.1',
        'user': user1,
        'port': 922,
        'password': '******'
    }, {
        'host': '127.0.0.1',
        'user': user1
    }, {
        'user': user1,
        'proto': 'command',
        'cmd': 'telnet 127.0.0.1',
        'can_change_prompt': False
    }]

    d = Device('localhost_name',
               connections=dict(
                   cmd={
                       'tunnel': dict(hops=hops),
                       'proto': 'command',
                       'cmd': 'telnet 127.0.0.1'
                   }),
               interfaces=dict(default={'ip': 'localhost'}),
               users=dict(learner=user1))
    try:
        with d.conn.open():

            if not d.check_output('whoami') == d.users.default.username:
                raise Exception
            else:
                assert True
                print(d.check_output('netstat -ant|grep -v TIME_WAIT'))
    except Exception:
        import pprint
        pprint.pprint(d.conn.get_all_conversations_flat())

        raise
Example #10
0
def test_put_on_all_ssh_tunnel_with_scp_on_device():
    hops = [
            {'host': '127.0.0.1', 'user': USERS['learner'], 'port': 922,},
            {'host': '127.0.0.1', 'user': USERS['learner']}
          ]

    d = Device('localhost',
               connections=dict(ssh={'file_transfer_protocol': 'scp'}),
               users=USERS,
               tunnels=dict(default=dict(hops=hops)))
    with d.conn.open(tunnel='default'):
        d.files.put(os.path.join(files_folder, 'conn_settings.yaml'))
Example #11
0
def test_get_on_mix_tunnel():
    hops = [
            {'host': '127.0.0.1', 'user': USERS['learner'], 'port': 922,},
            {'host': '127.0.0.1', 'user': USERS['learner']},
            {'host': '127.0.0.1', 'user': USERS['learner'], 'proto': 'telnet'}
          ]

    d = Device('localhost',
               connections=dict(ssh={'user': '******'}),
               users=USERS,
               tunnels=dict(default=dict(hops=hops)))
    with d.conn.open(tunnel='default'):
          d.files.get('conn_settings.yaml')
Example #12
0
def test_put_on_mix_tunnel():
    hops = [
            {'host': '127.0.0.1', 'user': USERS['learner'], 'port': 922,},
            {'host': '127.0.0.1', 'user': USERS['learner']},
            {'host': '127.0.0.1', 'user': USERS['learner'], 'proto': 'telnet'}
          ]

    d = Device('localhost',
               connections=dict(ssh={'user': '******'}),
               users=USERS,
               tunnels=dict(default=dict(hops=hops)))
    with d.conn.open(tunnel='default'):
        d.files.put(os.path.join(files_folder, 'conn_settings.yaml'))

    print(d.conn['ssh'].conversations_string())
Example #13
0
def test_saving_restoring_connections_multihop_to_db_reload():
    update_db_config(False, False)

    user1 = properties.UserInfo(username='******',
                                password='******',
                                expected_prompt=r'{username}@.+?:~\$ ')

    t0 = time.time()
    d2 = Device.get_by_host('localhost')
    with d2.conn.open():
        print(time.time() - t0)
        assert re.search(user1.expected_prompt.format(username='******'),
                         d2.conn.prompt_found.replace('\\', '')) is not None
    t0 = time.time()
    with d2.conn.open():
        print(time.time() - t0)
        assert re.search(user1.expected_prompt.format(username='******'),
                         d2.conn.prompt_found.replace('\\', '')) is not None
Example #14
0
def test_mix_tunnel_encrypted_keys_after_telnet_raises_notimplementederror():
    """ we raise an error if we try to send ssh key password using telnet """
    hops = [{
        'host': '127.0.0.1',
        'user': USERS['learner'],
        'port': 922,
    }, {
        'host': '127.0.0.1',
        'user': USERS['learner']
    }, {
        'host': '127.0.0.1',
        'user': USERS['learner'],
        'proto': 'telnet'
    }]

    d = Device('localhost',
               connections=dict(ssh={}),
               users=USERS,
               tunnels=dict(default=dict(hops=hops)))
    with pytest.raises(NotImplementedError):
        with d.conn.open(
                tunnel='default', user='******'
        ):  # using mysshuser for the connection after the tunnel
            pass