0

With paramiko.SSHClient(), how can I specify the network interface to bind to. I'm referring to this argument in the ssh man page:

 -B bind_interface
             Bind to the address of bind_interface before attempting to
             connect to the destination host.  This is only useful on
             systems with more than one address.

In other words, I want to ssh like this ssh -B en0 username@192.168.1.1 using paramiko.SSHClient().

James Arnold
  • 465
  • 4
  • 9

1 Answers1

0

Paramiko does not have native support for selecting the network interface.

But you can use sock parameter of SSHClient.connect to provide your own socket to use for the SSH connection.

For details how to bind Python socket to an interface, see:
How to bind socket to an interface in python (socket.SO_BINDTODEVICE missing)

I didn't test it, but it should be like this:

import paramiko
import socket
import IN

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, str("en0" + '\0').encode('utf-8'))
sock.connect(("192.168.1.1", 22))
ssh = paramiko.SSHClient()
ssh.connect("192.168.1.1", username=username, sock=sock, ...)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992