Manuel Rodriguez
Manuel Rodriguez

Reputation: 73

Paramiko BindAddress option

I am trying to use paramiko to connect to a remote host. However, in order to connect, I need to specify a bind address, which you can do with OpenSSH via:

ssh -o BindAddress=x.x.x.x user@host

I've been searching high and low for an equivalent option in the paramiko SSHClient docs, but I can't seem to find it. It seems like this would be a standard option to have. Can someone point me in the right direction? Do I need to create a separate socket connection and use that?

Upvotes: 4

Views: 1858

Answers (1)

gbajson
gbajson

Reputation: 1770

From ssh_config manual:

BindAddress Use the specified address on the local machine as the source address of the connection.

You might provide a socket to client's connect function, so you might bind the source address in the socket.

Example:

import socket
import paramiko

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sock.bind(('192.168.223.21', 0))           # set source address
sock.connect(('192.168.223.23', 22))       # connect to the destination address

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('192.168.223.23', 
               username=username, 
               password=password, 
               sock=sock)                  # pass socket to Paramiko

Documentation: http://docs.paramiko.org/en/2.4/api/client.html).

connect(hostname, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True, compress=False, sock=None, gss_auth=False, gss_kex=False, gss_deleg_creds=True, gss_host=None, banner_timeout=None, auth_timeout=None, gss_trust_dns=True, passphrase=None)

sock (socket) – an open socket or socket-like object (such as a Channel) to use for communication to the target host

Upvotes: 1

Related Questions