Dynamic port forwarding using SOCKS using paramiko library in python

I am trying to connect to an SSH server as follows:

import paramiko import socks sock = socks.socksocket() sock.setproxy(socks.PROXY_TYPE_SOCKS5, 'localhost', 22, True) sock.connect((**IP address of SSH server**, 22)) t = paramiko.Transport(sock) t.connect( None, 'username', 'password') 

And get the following error

 > Traceback (most recent call last): ... > sock.connect((**IP address of SSH server**, 22)) File "C:\Python27\lib\site-packages\socks.py", line 368, in connect > _orgsocket.connect(self,(self.__proxy[1],portnum)) File "C:\Python27\lib\socket.py", line 224, in meth > return getattr(self._sock,name)(*args) socket.error: [Errno 10061] No connection could be made because the target machi ne actively > refused it 

My goal is to simulate the Putty method in creating the SSH SOCKS Proxy, as here: Configure PuTTY to create the SSH SOCKS proxy for safe browsing . Or equivalently

Ssh port -D [localhost]

for local dynamic port forwarding at the application level.

Can someone please explain what is wrong and how to do it correctly with the help of paramiko? Thanks.

PS I found this https://stackoverflow.com/a/3186161/how-to-install-button-from-the-difference-between-statement-and-javascript/232837#432837 Somebody?

+4
source share
1 answer

Paramiko can connect to ssh. You do not need the SOCKS library to connect to the ssh server. In addition, when you try, the remote server refuses to connect because you are not authenticating.

The correct way to do this is to link to paramiko sshClient :

 import paramiko ssh = paramiko.SSHClient() ssh.connect('yourServer', username='you', password='yay!') 

And then get the base transport :

 trans = ssh.get_transport() 

Finally, ask the ssh client to forward the tcp port with the open channel :

 trans.open_channel("forwarded-tcpip", dest_addr=('serverIP',8000), src_addr=('localhost'),8000)) 

This will cause any connections on port 8000 to be redirected locally to port 8000 remotely through this ssh session.

+5
source

Source: https://habr.com/ru/post/1481959/


All Articles