Edwin Evans
Edwin Evans

Reputation: 2846

How to use Python to set up a tunnel matching an ssh command (for bastion)?

How would I set up a tunnel using Python code that could replace this command?

ssh -N -L 3307:xxxxxx.rds.amazonaws.com:3306 [email protected] -i ~/.ssh/bastion_key.pem

Upvotes: 1

Views: 383

Answers (1)

mVChr
mVChr

Reputation: 50205

You can use the sshtunnel library.

For example:

from sshtunnel import SSHTunnelForwarder

with SSHTunnelForwarder(
    ('XX.XXX.XX.XX', 22),
    ssh_username='ec2-user',
    ssh_pkey='~/.ssh/bastion_key.pem',
    remote_bind_address=('xxxxxx.rds.amazonaws.com', 3306),
    local_bind_address=('0.0.0.0', 3307)
) as tunnel:
    # do stuff with tunnel

Upvotes: 1

Related Questions