使用 Tor 代理的 Python ssh [英] Python ssh using Tor proxy

查看:81
本文介绍了使用 Tor 代理的 Python ssh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从 Python 脚本使用 ssh 时,我希望能够通过 Tor 发送数据.当我使用 OpenSSH 客户端手动 ssh 到主机时,Tor 按预期工作.这是我的 ssh 配置文件.我使用带有 ProxyCommand 的连接代理通过 Tor 路由连接(同样,这可以通过标准 OpenSSH 客户端正常工作):

I would like to be able to send data through Tor when I use ssh from Python scripts. Tor works as expected when I use an OpenSSH client to manually ssh to the host. This is my ssh config file. I use connect-proxy with ProxyCommand to route the connections through Tor (again, this works fine via a standard OpenSSH client):

host host
user user
hostname host.domain.com
CheckHostIP no
Compression yes
Protocol 2
ProxyCommand connect-proxy -S localhost:9050 %h %p

我有这个 Python 测试脚本:

I have this Python test script:

import paraproxy
import paramiko

conf = paramiko.SSHConfig()
conf.parse(open('/home/user/.ssh/config'))
host = conf.lookup('host')
print host

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host["hostname"], username=host["user"], password='test')
client.close()

此脚本对主机执行 ssh,但是,它不使用 ssh 配置文件中的 ProxyCommand,因此它不会通过 Tor 路由流量.我尝试了几种不同的配置,但我无法使其工作.关于如何使这项工作的任何想法?

This script does ssh to the host, however, it does not use the ProxyCommand in the ssh config file, thus it does not route traffic through Tor. I've tried a few different configurations, but I cannot make it work. Any ideas on how to make this work?

推荐答案

您需要创建 ProxyCommand(一个类似套接字的对象)并将其传递给 client.connect()代码>

You need to create ProxyCommand (a socket-like object) and pass it to client.connect()

import paramiko

conf = paramiko.SSHConfig()
conf.parse(open('/home/user/.ssh/config'))
host = conf.lookup('host')
print host

proxy = paramiko.ProxyCommand(host['proxycommand'])

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host["hostname"], username=host["user"], password='test', sock=proxy)
client.close()

connect() 文档方法.注意timeout 参数.如果您正在执行一些自动化操作,指定它总是一个好主意.

Docs for connect() method. Note timeout parameter. It's always a good idea to specify it if you are doing some automation.

这篇关于使用 Tor 代理的 Python ssh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆