質問

I'm trying to connect to SSH server in the following way:

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 Putty's way in creating SSH SOCKS Proxy as here: Configure PuTTY To Create SSH SOCKS Proxy For Secure Browsing. Or equivalent

ssh -D [localhost]port

for local dynamic application-level port forwarding.

Can someone explain, please, what's wrong and how to do it the right way using paramiko? Thanks.

P.S. I've found this https://stackoverflow.com/a/5823383/1264304 However, I don't succeed to implement it. Someone?

役に立ちましたか?

解決

Paramiko can natively connect to ssh. You don't need the SOCKS library to connect to the ssh server. Additionally, when you try, the remote server refuses to connect because you don't authenticate.

The proper way to do this would be to connect with paramiko's sshClient:

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

And then, get the underlying transport:

trans = ssh.get_transport()

Finally, have the ssh client forward a tcp port with open channel:

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

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top