SSHTUNNEL(1) sshtunnel SSHTUNNEL(1) (NAME) python-sshtunnel - SSH [image: CircleCI] [image] [image: AppVeyor] [image] [image: Documentation Status] [image] [image: coveralls] [image] [image: version] [image] [image: pyversions] [image] [image: license] [image] ?B?P: Pahaz ?B?P: Windows . : (REQUIREMENTS) o paramiko sshtunnel PyPI : pip install sshtunnel easy_install sshtunnel conda install -c conda-forge sshtunnel : python setup.py install (TESTING THE PACKAGE) tox : python setup.py test ?Bsshtunnel?P . ( ) SSH ( ) . ---------------------------------------------------------------------- | -------------+ | +----------+ LOCAL | | | REMOTE | :22 SSH CLIENT | <== SSH ========> | SERVER | :8080 web service -------------+ | +----------+ | FIREWALL (only port 22 is open) ---------------------------------------------------------------------- ?B ?P: SSH. SSH ( ?BREMOTE SERVER?P) ( ?BLOCAL CLIENT?P) . ---------------------------------------------------------------------- | -------------+ | +----------+ +--------- LOCAL | | | REMOTE | | PRIVATE CLIENT | <== SSH ========> | SERVER | <== local ==> | SERVER -------------+ | +----------+ +--------- | FIREWALL (only port 443 is open) ---------------------------------------------------------------------- ?B ?P: ?BPRIVATE SERVER?P SSH. (API) (context) ?Bwith?P ?B ?P : (EXAMPLE 1) ?B ?P ?Bpahaz.urfuclub.ru?P . from sshtunnel import SSHTunnelForwarder server = SSHTunnelForwarder( 'alfa.8iq.dev', ssh_username="pahaz", ssh_password="secret", remote_bind_address=('127.0.0.1', 8080) ) server.start() print(server.local_bind_port) # show assigned local port # work with `SECRET SERVICE` through `server.local_bind_port`. server.stop() (EXAMPLE 2) (pkey) SSH (?B ?P): import paramiko import sshtunnel with sshtunnel.open_tunnel( (REMOTE_SERVER_IP, 443), ssh_username="", ssh_pkey="/var/ssh/rsa_key", ssh_private_key_password="secret", remote_bind_address=(PRIVATE_SERVER_IP, 22), local_bind_address=('0.0.0.0', 10022) ) as tunnel: client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('127.0.0.1', 10022) # do some operations with client session client.close() print('FINISH!') (EXAMPLE 3) MySQL Vagrant: from sshtunnel import open_tunnel from time import sleep with open_tunnel( ('localhost', 2222), ssh_username="vagrant", ssh_password="vagrant", remote_bind_address=('127.0.0.1', 3306) ) as server: print(server.local_bind_port) while True: # press Ctrl-C for stopping sleep(1) print('FINISH!') (CLI): (bash)$ python -m sshtunnel -U vagrant -P vagrant -L :3306 -R 127.0.0.1:3306 -p 2222 localhost (EXAMPLE 4) SSH . SSH . import sshtunnel from paramiko import SSHClient with sshtunnel.open_tunnel( ssh_address_or_host=('GW1_ip', 20022), remote_bind_address=('GW2_ip', 22), ) as tunnel1: print('Connection to tunnel1 (GW1_ip:GW1_port) OK...') with sshtunnel.open_tunnel( ssh_address_or_host=('localhost', tunnel1.local_bind_port), remote_bind_address=('target_ip', 22), ssh_username='GW2_user', ssh_password='GW2_pwd', ) as tunnel2: print('Connection to tunnel2 (GW2_ip:GW2_port) OK...') with SSHClient() as ssh: ssh.connect('localhost', port=tunnel2.local_bind_port, username='target_user', password='target_pwd', ) ssh.exec_command(...) $ sshtunnel --help usage: sshtunnel [-h] [-U SSH_USERNAME] [-p SSH_PORT] [-P SSH_PASSWORD] -R IP:PORT [IP:PORT ...] [-L [IP:PORT [IP:PORT ...]]] [-k SSH_HOST_KEY] [-K KEY_FILE] [-S KEY_PASSWORD] [-t] [-v] [-V] [-x IP:PORT] [-c SSH_CONFIG_FILE] [-z] [-n] [-d [FOLDER [FOLDER ...]]] ssh_address Pure python ssh tunnel utils Version 0.4.0 positional arguments: ssh_address SSH server IP address (GW for SSH tunnels) set with "-- ssh_address" if immediately after -R or -L optional arguments: -h, --help show this help message and exit -U SSH_USERNAME, --username SSH_USERNAME SSH server account username -p SSH_PORT, --server_port SSH_PORT SSH server TCP port (default: 22) -P SSH_PASSWORD, --password SSH_PASSWORD SSH server account password -R IP:PORT [IP:PORT ...], --remote_bind_address IP:PORT [IP:PORT ...] Remote bind address sequence: ip_1:port_1 ip_2:port_2 ... ip_n:port_n Equivalent to ssh -Lxxxx:IP_ADDRESS:PORT If port is omitted, defaults to 22. Example: -R 10.10.10.10: 10.10.10.10:5900 -L [IP:PORT [IP:PORT ...]], --local_bind_address [IP:PORT [IP:PORT ...]] Local bind address sequence: ip_1:port_1 ip_2:port_2 ... ip_n:port_n Elements may also be valid UNIX socket domains: /tmp/foo.sock /tmp/bar.sock ... /tmp/baz.sock Equivalent to ssh -LPORT:xxxxxxxxx:xxxx, being the local IP address optional. By default it will listen in all interfaces (0.0.0.0) and choose a random port. Example: -L :40000 -k SSH_HOST_KEY, --ssh_host_key SSH_HOST_KEY Gateway's host key -K KEY_FILE, --private_key_file KEY_FILE RSA/ECDSA private key file -S KEY_PASSWORD, --private_key_password KEY_PASSWORD RSA/ECDSA private key password -t, --threaded Allow concurrent connections to each tunnel -v, --verbose Increase output verbosity (default: ERROR) -V, --version Show version number and quit -x IP:PORT, --proxy IP:PORT IP and port of SSH proxy to destination -c SSH_CONFIG_FILE, --config SSH_CONFIG_FILE SSH configuration file, defaults to ~/.ssh/config -z, --compress Request server for compression over SSH transport -n, --noagent Disable looking for keys from an SSH agent -d [FOLDER [FOLDER ...]], --host_pkey_directories [FOLDER [FOLDER ...]] List of directories where SSH pkeys (in the format `id_*`) may be found ?Isshtunnel?P - SSH . ?Bsshtunnel?P SSH . () ?BSSHTunnelForwarder.stop()?P (context) . sshtunnel.SSH_TIMEOUT = 0.1 ( ) (?Bsocket.settimeout?P) sshtunnel.TUNNEL_TIMEOUT = 10.0 ( ) ( open_channel) sshtunnel.DEFAULT_LOGLEVEL = 40 (ERROR) sshtunnel.DEFAULT_SSH_DIRECTORY = '~/.ssh' ssh sshtunnel.check_address(address) : address (tuple): (?Bstr?P, ?Bint?P) IP ?B:?P ?Baddress?P ?Bstr?P (Raises): ValueError: : >>> check_address(('127.0.0.1', 22)) sshtunnel.check_addresses(address_list, is_remote=False) : address_list (list[tuple]): (?Bstr?P, ?Bint?P) IP ?B:?P ?Bstr?P is_remote (boolean): (Raises): AssertionError: ?Baddress_list?P ValueError: : >>> check_addresses([('127.0.0.1', 22), ('127.0.0.1', 2222)]) sshtunnel.create_logger(logger=None, loglevel=None, capture_warnings=True, add_paramiko_handler=True) (logger) : logger (Optional[logging.Logger]): ?Blogging.Logger?P loglevel (Optional[str or int]): ?Blogging.Logger?P ( ?BERROR?P) (10 == ?BDEBUG?P) ?B:?P 1 == ?BTRACE?P (Tracing) capture_warnings (boolean): / warnings ?Blogger?P : True ?B:?P . add_paramiko_handler (boolean): ?Bparamiko.transport?P : True : ?Blogging.Logger?P exception sshtunnel.BaseSSHTunnelForwarderError(*args, **kwargs) ?BSSHTunnelForwarder?P exception sshtunnel.HandlerSSHTunnelForwarderError(*args, **kwargs) class sshtunnel.SSHTunnelForwarder(ssh_address_or_host=None, ssh_config_file='~/.ssh/config', ssh_host_key=None, ssh_password=None, ssh_pkey=None, ssh_private_key_password=None, ssh_proxy=None, ssh_proxy_enabled=True, ssh_username=None, local_bind_address=None, local_bind_addresses=None, logger=None, mute_exceptions=False, remote_bind_address=None, remote_bind_addresses=None, set_keepalive=5.0, threaded=True, compression=None, allow_agent=True, host_pkey_directories=None, *args, **kwargs) ?B SSH?P o SSH o : o SSH ( ?B~/.ssh/config?P) o SSH ( Pageant GNOME Keyring) (Raises): ?BBaseSSHTunnelForwarderError?P: SSHTunnelForwarder ?BHandlerSSHTunnelForwarderError?P: ?B:?P ?Bmute_exceptions?P ?Braise_exception_if_any_forwarder_have_a_problem?P () : ssh_address_or_host (tuple or str): IP ?BREMOTE GATEWAY?P. (?Bstr?P, ?Bint?P) IP ?Bstr?P IP 0.0.4. ssh_config_file (str): SSH . ?BNone?P : ?BSSH_CONFIG_FILE?P 0.0.4. ssh_host_key (str): <> OpenSSH. ?BREMOTE GATEWAY?P SSH . ( ) ssh_username (str): ?BREMOTE SERVER?P : ssh_password (str): ?BREMOTE SERVER?P . ?B:?P ssh_port (int): SSH ?BREMOTE GATEWAY?P ?Issh_address_or_host`?P ?Bstr?P IP ?BREMOTE GATEWAY?P : 22 ssh_pkey (str or paramiko.PKey): ?B?P (?Bstr?P) ?B?P (?Bparamiko.pkey.PKey?P ) ssh_private_key_password (str): ?Bssh_pkey?P ?B:?P ssh_proxy (socket-like object or tuple): SSH . ?Bparamiko.proxy.ProxyCommand?P . sock ?Bparamiko.transport.Transport?P ?BProxyCommand?P ?Bssh_config(5)?P . (?Bstr?P, ?Bint?P) IP ?B:?P ?Bssh_proxy_enabled?P False 0.0.5. ssh_proxy_enabled (boolean): / SSH. True ?Bssh_config_file?P ?BProxyCommand?P ?Bssh_address_or_host?P ?Bparamiko.proxy.ProxyCommand?P SSH : ?BTrue?P 0.0.4. local_bind_address (tuple): (?Bstr?P, ?Bint?P) IP . ?B('', 8000)?P ?B('10.0.0.1', )?P : ?B('0.0.0.0', RANDOM_PORT)?P 0.0.8: bind local_bind_addresses (list[tuple]): ( ?Blocal_bind_address?P) [(ip1, port_1), (ip_2, port2), ...] : ?B[local_bind_address]?P 0.0.4. remote_bind_address (tuple): (?Bstr?P, ?Bint?P) IP . remote_bind_addresses (list[tuple]): ( ?Bremote_bind_address?P) [(ip1, port_1), (ip_2, port2), ...] : ?B[remote_bind_address]?P 0.0.4. allow_agent (boolean): / SSH : ?BTrue?P 0.0.8. host_pkey_directories (list): pkey ['~/.ssh']. : ?BNone?P () 0.1.4. compression (boolean): / . : ?BFalse?P 0.0.8. logger (logging.Logger): sshtunnel paramiko : ?Blogging.Logger?P ?Blogging.StreamHandler?P ?BDEFAULT_LOGLEVEL?P 0.0.3. mute_exceptions (boolean): ?BBaseSSHTunnelForwarderError?P ?BHandlerSSHTunnelForwarderError?P : ?BFalse?P 0.0.8. set_keepalive (float): ?I'keepalive'?P ( ). NAT . keepalive 0.0 . : 5.0 ( keepalive ) 0.0.7. threaded (boolean): : ?BTrue?P 0.0.3. ssh_address (str): ?Bssh_address_or_host?P (str, int) IP ?BREMOTE SERVER?P 0.0.4. ssh_host (str): ?Bssh_address_or_host?P (str, int) IP ?BREMOTE SERVER?P 0.0.4. ssh_private_key (str or paramiko.PKey): ?Bssh_pkey?P ?B?P (?Bstr?P) ?B?P (?Bparamiko.pkey.PKey?P ) 0.0.8. raise_exception_if_any_forwarder_have_a_problem (boolean): ?BBaseSSHTunnelForwarderError?P ?BHandlerSSHTunnelForwarderError?P False : ?BTrue?P 0.0.4. 0.0.8: ( ?Bmute_exceptions?P ) (Attributes): tunnel_is_up (dict): ( ) ( ) ?B:?P ?B:?P ?Bskip_tunnel_checkup?P ?BTrue?P ?B?P: {('127.0.0.1', 55550): True, # this tunnel is up ('127.0.0.1', 55551): False} # this one isn't 55550 55551 bind skip_tunnel_checkup (boolean): ( ). 0.1.0. daemon_forward_servers = True (daemon) daemon_transport = True SSH (daemon) local_is_up(target) ( TCP ) : target (tuple): (?Bstr?P, ?Bint?P) IP : boolean 0.1.0: ?Bcheck_tunnels()?P ?Btunnel_is_up?P check_tunnels() ?Btunnel_is_up?P static get_agent_keys(logger=None) SSH : logger (Optional[logging.Logger]) : list static get_keys(logger=None, host_pkey_directories=None, allow_agent=False) SSH .ssh. : logger (Optional[logging.Logger]) host_pkey_directories (Optional[list[str]]): pkey SSH "id_*" . ['~/.ssh'] 0.1.0. allow_agent (Optional[boolean]): SSH : False : list static read_private_key_file(pkey_file, pkey_password=None, key_type=None, logger=None) SSH : pkey_file (str): (RSA ECDSA) : pkey_password (Optional[str]): logger (Optional[logging.Logger]) : paramiko.Pkey start() SSH stop(force=False) . . ?Iforce=True?P : force (bool): : False 0.2.2. ?B:?P ?B0.1.0?P ?B?P : o o o ( ?BSYN?P ?BFIN?P ... ) o : ?BFIN_WAIT_2?P ?BCLOSE_WAIT?P . ?B:?P ?Btunnel_is_up?P : False ?Bshutdown()?P close() ?Bstop()?P restart() property local_bind_ports TCP property local_bind_hosts IP property local_bind_addresses (IP, port) property tunnel_bindings tunnel_bindings local<>remote property is_active True SSH sshtunnel.open_tunnel(*args, **kwargs) SSH (wrapper) ?BSSHTunnelForwarder?P : destination (Optional[tuple]): IP SSH (?Bssh_address?P, ?Bssh_port?P) : debug_level (Optional[int or str]): ?Blogging.Logger?P ?BDEBUG?P skip_tunnel_checkup (boolean): / ?Btunnel_is_up?P : True 0.1.0. ?B:?P ?Bdebug_level?P 1 == ?BTRACE?P ?B:?P ?BSSHTunnelForwarder?P ?B?P: from sshtunnel import open_tunnel with open_tunnel(SERVER, ssh_username=SSH_USER, ssh_port=22, ssh_password=SSH_PASSWORD, remote_bind_address=(REMOTE_HOST, REMOTE_PORT), local_bind_address=('', LOCAL_PORT)) as server: def do_something(port): pass print("LOCAL PORTS:", server.local_bind_port) do_something(server.local_bind_port) o Cameron Maske o Gustavo Machado o Colin Jermain o JM Fernandez - ( !) o Lewis Thompson o Erik Rogers o Mart Somermaa o Chronial o Dan Harbin o Ignacio Peluffo o Niels Zeilemaker o Georgy Rylov o Eddie Chiang o kkrasovskii o v.0.4.0 (Pahaz ) o daemon ( ) (#219 ) o Mongo/Postgres/MySQL (#219 ) o (#219 ) o v.0.3.2 (Pahaz , JM Fernandez ) o o ssh ?I~/.ssh?P o v.0.3.1 (Pahaz ) o o v.0.3.0 (Pahaz ) o context with ?I.stop(force=True)?P ( ) o ?Idaemon_forward_servers = True?P ( ) o keepalive ( 0.3.0 ) o 0.1 o ?Iblock_on_close?P o (deadlocks) / (#173 ) o v.0.2.2 (Pahaz ) o ?I.stop(force=True)?P (#201 ) o v.0.2.1 (Pahaz , Eddie Chiang and kkrasovskii ) o (DOWN) (#170 ) o v.0.2.0 (Georgy Rylov ) o IPv6 . paramiko . IPv6 IPv4 (#139 ) o v.0.1.5 (JM Fernandez ) o ?Iblock_on_close?P o v.0.1.4 (Niels Zeilemaker ) o pkey ?I~/.ssh?P o v.0.1.3 (Ignacio Peluffo and others) o ?Bpkey_file?P ?B~?P o o v.0.1.2 (JM Fernandez ) o #77 o v.0.1.1 (JM Fernandez ) o #72 o v.0.1.0 (JM Fernandez ) o ?Itunnel_bindings?P o (#49, #56, #57, #59, #60, #62, #64, #66, ...) (Pahaz , JM Fernandez ) o TRACE (JM Fernandez ) o (JM Fernandez ) o python3.2 o v.0.0.8 (JM Fernandez ) o #31 : () (Dan Harbin ) o API (JM Fernandez ) o (JM Fernandez ) o ?Ballow_agent?P ( #36 , #46 ) (JM Fernandez ) o ?Bcompression?P (JM Fernandez ) o ?B__str__?P (JM Fernandez ) o (JM Fernandez ) o ssh_config (JM Fernandez ) o IP (JM Fernandez ) o (JM Fernandez ) o AppVeyor (JM Fernandez ) o v.0.0.7 (JM Fernandez ) o (#41 ) (JM Fernandez ) o SSH keep-alive (#29 ) (JM Fernandez ) o pkey (#43 ) (Chronial ) o ?B-V?P CLI (JM Fernandez ) o (JM Fernandez ) o (JM Fernandez ) o v.0.0.6 (Pahaz ) o ?B-S?P CLI ssh (Pahaz ) o v.0.0.5 (Pahaz ) o ?Bssh_proxy?P ?BProxyCommand?P ?Bssh_config(5)?P (Lewis Thompson ) o . (Mart Somermaa ) o ?BSSHTunnelForwarder?P ?Bparamiko.transport?P (JM Fernandez ) o #34 #33 (Pahaz ) o (Pahaz ) o CI (Pahaz ) o (Pahaz ) o ?BSSHTunnelForwarder.local_is_up?P (Pahaz ) o daemon mode = False : o v.0.0.4.4 (Pahaz ) o #24 - ssh (Pahaz ) o v.0.0.4.3 (Pahaz ) o #19 (Pahaz ) o v.0.0.4.2 (Pahaz ) o Thread.daemon < 3.3 #16 (Pahaz ) o v.0.0.4.1 (Pahaz ) o CLI #13 (Pahaz ) o v.0.0.4 (Pahaz ) o daemon (JM Fernandez , Pahaz ) o ?Bmake_ssh_forward_server?P ?BSSHTunnelForwarder.make_ssh_forward_server?P (Pahaz ) o ?Bmake_ssh_forward_handler?P ?BSSHTunnelForwarder.make_ssh_forward_handler_class?P (Pahaz ) o ?Bopen?P ?Bopen_tunnel?P (JM Fernandez ) - ! o CLI (JM Fernandez ) o (JM Fernandez ) o (JM Fernandez , Pahaz ) o (JM Fernandez , Pahaz ) o ?Braise_exception_if_any_forwarder_have_a_problem?P (Pahaz ) o ?Bssh_config_file?P (JM Fernandez ) o Python 3 (JM Fernandez , Pahaz ) o v.0.0.3 (Pahaz ) o ?Bthreaded?P (Cameron Maske ) o (Gustavo Machado ) o ?Bpip install?P (Colin Jermain , Pahaz ) o v.0.0.1 (Pahaz ) o ?BSSHTunnelForwarder?P (Pahaz ) o ?Bopen?P (Pahaz ) (c) 2014-2019 Pahaz White ( <<>>) / . << >> . . (AUTHOR) Pahaz White (COPYRIGHT) 2014-2020, Pahaz White and contributors 0.4.0 December 24, 2025 SSHTUNNEL(1)