Kevin Arévalo
Kevin Arévalo

Reputation: 41

Paramiko session scope

I have some doubts about Paramiko. If I do the standard code found in internet:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='remote_host', username='...', password='...')
// TODO
ssh.close()

Let's say I have some kind of JDBC connector and I try to connect to a database placing the code below within the TODO section

ssh.connect(hostname='remote_host', username='...', password='...')
connector.connect(database_params...)
ssh.close()

My question is. In that case the client of the database server would be remote_host or my local machine where the Python script is being executed?


Or the right way would be using sshtunnel? like so:

with open_tunnel(
    ('remote_host', '22'),
    ssh_username=...,
    ssh_password=...,
    remote_bind_address=('DB_ADDRESS', 'DB_PORT')
    local_bind_address=('SOME_IP', 'SOME_PORT')
) as server:
    connector.connect(db_host='SOME_IP', db_port='SOME_PORT', ...)

Is there any difference between those two approaches?
Thanks in advance.

Upvotes: 0

Views: 344

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202504

Opening SSH connection using Paramiko or any other way, has no effect whatsoever on your database connection, nor any other connections, nor a file access, nor command execution. So your first code has no chance of doing, what you want.

Everything you want to do via the SSH connection, you need to do via the Paramiko API.

And that's what open_tunnel does, internally.

For an equivalent standalone code, see forward_tunnel function in Paramiko forward.py demo.

Upvotes: 2

Related Questions