gpt4 book ai didi

python - 通过SOCKS5代理连接SSH服务器

转载 作者:行者123 更新时间:2023-12-02 14:26:58 32 4
gpt4 key购买 nike

如何使用共享 key 与python中的socks通过ssh连接?
我已经测试了几种方法,有什么建议吗?
非常感谢您提前...
进口:

import paramiko
from paramiko import RSAKey
import socket
import socks
import base64


脚本:
def createSSHClient(server, port, user):

sock=socks.socksocket()
sock.set_proxy(
proxy_type=socks.SOCKS5,
addr='10.0.0.2',
port=1080,
username=base64.b64decode('dXNlcg==').decode("utf-8"),
password=base64.b64decode('cGFzc3dvcmQ=').decode("utf-8")
)

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privkey = paramiko.RSAKey.from_private_key_file('/home/my-user/.ssh/id_rsa')
ssh.connect(server, port, user, privkey, sock)
return ssh

outputsocks = []
ssh = createSSHClient('192.168.1.23', 22, 'my-user')
outputsocks.append((ssh.exec_command('ls -ltr')[1]).read().decode('ascii'))
ssh.close()

print(outputsocks)
输出:
Traceback (most recent call last):
File "/home/my-user/teste2.py", line 26, in <module>
ssh = createSSHClient('192.168.1.23', 22, 'my-user')
File "/home/my-user/teste2.py", line 22, in createSSHClient
ssh.connect(server, port, user, privkey, sock)
File "/usr/local/lib/python3.6/site-packages/paramiko/client.py", line 349, in connect
retry_on_signal(lambda: sock.connect(addr))
File "/usr/local/lib/python3.6/site-packages/paramiko/util.py", line 283, in retry_on_signal
return function()
File "/usr/local/lib/python3.6/site-packages/paramiko/client.py", line 349, in <lambda>
retry_on_signal(lambda: sock.connect(addr))
TimeoutError: [Errno 110] Connection timed out
注:如果您通过socks 删除连接并使用不需要VPN 的主机,则相同的脚本会起作用,socks 凭据正确且共享 key 有效。

最佳答案

错误是在启动 SSH 之前没有通过 sock 打开连接,这已通过 sock.connect() 解决。
更多信息: socket connect() vs bind()
错误修复:

def createSSHClient(server, port, user):

sock=socks.socksocket()
sock.set_proxy(
proxy_type=socks.SOCKS5,
addr='10.0.0.2',
port=1080,
username=base64.b64decode('dXNlcg==').decode("utf-8"),
password=base64.b64decode('cGFzc3dvcmQ=').decode("utf-8")
)
sock.connect(('192.168.1.20',22))

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privkey = paramiko.RSAKey.from_private_key_file('/home/my-user/.ssh/id_rsa')
ssh.connect(server, port, user, privkey, sock)
return ssh

outputsocks = []
ssh = createSSHClient('192.168.1.23', 22, 'my-user')
outputsocks.append((ssh.exec_command('ls -ltr')[1]).read().decode('ascii'))
ssh.close()
sock.close()

print(outputsocks)

关于python - 通过SOCKS5代理连接SSH服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62523323/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com