How to use the sshtunnel.SSHTunnelForwarder function in sshtunnel

To help you get started, we’ve selected a few sshtunnel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github chaibio / chaipcr / test / chai_remote.py View on Github external
def sql_command(self, command):
        """Execute an sql command and return the results.
        
        The db connection is established over an ssh tunnel
        """

        try:
            with sshtunnel.SSHTunnelForwarder(
                    (self._config['host'], 22),
                    ssh_username = self._config['ssh_user'],
                    ssh_password = self._config['ssh_passwd'],
                    remote_bind_address=('127.0.0.1', 3306),
                    local_bind_address=('0.0.0.0', 8306)
                ) as tunnel:
                db = MySQLdb.connect(
                        host = '127.0.0.1',
                        port = 8306,
                        user = 'root',
                        passwd = '',
                        db = 'chaipcr'
                        )
                data = pd.read_sql(command+';', con=db)
                db.close()
                return data
github Azure / azure-cli / src / azure-cli / azure / cli / command_modules / batchai / custom.py View on Github external
def _create_tunnel(remote_host, port, username, password, ssh_private_key, local_addresses, remote_addresses, func):
    """Creates a tunnel to the remote host and runs provided func under the tunnel.

    :param str remote_host: ip or address of the remote host
    :param int port: the ssh port number
    :param str username: username to login under
    :param str or None password: the user password
    :param str or None ssh_private_key: the path to private ssh key
    :param local_addresses: local addresses to be forwarded
    :param remote_addresses: target addresses
    :param func: a function to run on the remote host. The forwarding is stopped as soon as func completes execution.
    """
    from sshtunnel import SSHTunnelForwarder
    local_addresses = [(a[0], a[1] if a[1] != 0 else _get_available_local_port()) for a in local_addresses]
    with SSHTunnelForwarder((remote_host, port),
                            ssh_username=username,
                            ssh_password=password,
                            ssh_pkey=ssh_private_key,
                            remote_bind_addresses=remote_addresses,
                            local_bind_addresses=local_addresses):
        func()
github garstka / idact / idact / detail / tunnel / build_tunnel.py View on Github external
def create_last_tunnel():
                return FirstHopTunnel(
                    forwarder=SSHTunnelForwarder(
                        ssh_address_or_host,
                        ssh_config_file=None,
                        ssh_username=config.user,
                        ssh_password=ssh_password,
                        ssh_pkey=ssh_pkey,
                        local_bind_address=local_bind_address,
                        remote_bind_address=remote_bind_address,
                        set_keepalive=TUNNEL_KEEPALIVE,
                        allow_agent=False,
                        logger=logger),
                    there=bindings[-1].port,
                    config=config)
github sirca / clusterous / clusterous / helpers.py View on Github external
def __init__(self, host, username, key_file, remote_port, host_port=22):
        """
        Returns tuple consisting of local port and sshtunnel SSHTunnelForwarder object.
        Caller must call stop() on object when finished
        """
        logger = logging.getLogger('sshtunnel')
        logger.setLevel(logging.ERROR)

        self._server = SSHTunnelForwarder((host, host_port),
                ssh_username=username, ssh_private_key=key_file,
                remote_bind_address=('127.0.0.1', remote_port), logger=logger)
github OmniDB / OmniDB / OmniDB / OmniDB_app / include / Session.py View on Github external
try:
                        if self.v_databases[p_database_index]['tunnel']['key'].strip() != '':
                            v_file_name = '{0}'.format(str(time.time())).replace('.','_')
                            v_full_file_name = os.path.join(settings.TEMP_DIR, v_file_name)
                            with open(v_full_file_name,'w') as f:
                                f.write(self.v_databases[p_database_index]['tunnel']['key'])
                            server = SSHTunnelForwarder(
                                (self.v_databases[p_database_index]['tunnel']['server'], int(self.v_databases[p_database_index]['tunnel']['port'])),
                                ssh_username=self.v_databases[p_database_index]['tunnel']['user'],
                                ssh_private_key_password=self.v_databases[p_database_index]['tunnel']['password'],
                                ssh_pkey = v_full_file_name,
                                remote_bind_address=(self.v_databases[p_database_index]['database'].v_active_server, int(self.v_databases[p_database_index]['database'].v_active_port)),
                                logger=logger
                            )
                        else:
                            server = SSHTunnelForwarder(
                                (self.v_databases[p_database_index]['tunnel']['server'], int(self.v_databases[p_database_index]['tunnel']['port'])),
                                ssh_username=self.v_databases[p_database_index]['tunnel']['user'],
                                ssh_password=self.v_databases[p_database_index]['tunnel']['password'],
                                remote_bind_address=(self.v_databases[p_database_index]['database'].v_active_server, int(self.v_databases[p_database_index]['database'].v_active_port)),
                                logger=logger
                            )
                        server.set_keepalive = 120
                        server.start()

                        s = SessionStore(session_key=self.v_user_key)
                        tunnels[self.v_databases[p_database_index]['database'].v_conn_id] = server

                        self.v_databases[p_database_index]['tunnel_object'] = str(server.local_bind_port)
                        self.v_databases[p_database_index]['database'].v_connection.v_host = '127.0.0.1'
                        self.v_databases[p_database_index]['database'].v_connection.v_port = server.local_bind_port
github SMAPPNYU / smapputil / py / ssh_tunnel / ssh_tunnel.py View on Github external
def start_ssh_tunnel(loginhost, login_username, login_password, localhost, localport, remotehost, remoteport):
    logger = logging.getLogger(__name__)

    if login_password:
        tunnel = sshtunnel.SSHTunnelForwarder(
            (loginhost, 22),
            ssh_username=login_username,
            ssh_password=login_password,
            local_bind_address=(localhost,int(localport)),
            remote_bind_address=(remotehost, int(remoteport))
        )
    else:
        tunnel = sshtunnel.SSHTunnelForwarder(
            (loginhost, 22),
            ssh_username=login_username,
            local_bind_address=(localhost,int(localport)),
            remote_bind_address=(remotehost, int(remoteport))
        )

    tunnel.start()
    return tunnel
github garstka / idact / idact / detail / tunnel / build_tunnel.py View on Github external
def create_middle_tunnel():
                    next_binding_port = next_binding.port  # noqa, pylint: disable=cell-var-from-loop, line-too-long
                    return FirstHopTunnel(
                        forwarder=SSHTunnelForwarder(
                            ssh_address_or_host,
                            ssh_config_file=None,
                            ssh_username=config.user,
                            ssh_password=ssh_password,
                            ssh_pkey=ssh_pkey,
                            local_bind_address=local_bind_address,
                            remote_bind_address=remote_bind_address,
                            set_keepalive=TUNNEL_KEEPALIVE,
                            allow_agent=False,
                            logger=logger),
                        there=next_binding_port,
                        config=config)
github OmniDB / OmniDB / OmniDB / OmniDB_app / include / Session.py View on Github external
v_tunnel_object = tunnels[self.v_databases[p_database_index]['database'].v_conn_id]
                        if not v_tunnel_object.is_active:
                            v_tunnel_object.stop()
                            v_create_tunnel = True
                    except Exception as exc:
                        v_create_tunnel = True
                        None

                if self.v_databases[p_database_index]['tunnel_object'] == None or v_create_tunnel:
                    try:
                        if self.v_databases[p_database_index]['tunnel']['key'].strip() != '':
                            v_file_name = '{0}'.format(str(time.time())).replace('.','_')
                            v_full_file_name = os.path.join(settings.TEMP_DIR, v_file_name)
                            with open(v_full_file_name,'w') as f:
                                f.write(self.v_databases[p_database_index]['tunnel']['key'])
                            server = SSHTunnelForwarder(
                                (self.v_databases[p_database_index]['tunnel']['server'], int(self.v_databases[p_database_index]['tunnel']['port'])),
                                ssh_username=self.v_databases[p_database_index]['tunnel']['user'],
                                ssh_private_key_password=self.v_databases[p_database_index]['tunnel']['password'],
                                ssh_pkey = v_full_file_name,
                                remote_bind_address=(self.v_databases[p_database_index]['database'].v_active_server, int(self.v_databases[p_database_index]['database'].v_active_port)),
                                logger=logger
                            )
                        else:
                            server = SSHTunnelForwarder(
                                (self.v_databases[p_database_index]['tunnel']['server'], int(self.v_databases[p_database_index]['tunnel']['port'])),
                                ssh_username=self.v_databases[p_database_index]['tunnel']['user'],
                                ssh_password=self.v_databases[p_database_index]['tunnel']['password'],
                                remote_bind_address=(self.v_databases[p_database_index]['database'].v_active_server, int(self.v_databases[p_database_index]['database'].v_active_port)),
                                logger=logger
                            )
                        server.set_keepalive = 120