我試圖通過SSH連接到通過另一臺計算機在Python中使用paramiko進行隧道傳輸的計算機,但我遇到了一些奇怪的問題。 在/.ssh/config
我的配置文件看起來像這樣:paramiko.Proxycommand無法設置套接字
Host remoteA
HostName 169.254.1.1
User root
IdentityFile ~/.ssh/id_dss.openssh.remoteA
ForwardX11 no
StrictHostKeyChecking no
ForwardAgent yes
UserKnownHostsFile /dev/null
Host remoteB
User root
IdentityFile ~/.ssh/id_dss.openssh.remoteB
ForwardX11 no
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
ProxyCommand ssh -W 169.254.1.2:22 remoteA
而且我的Python代碼:
from paramiko import SSHClient, SSHConfig, SSHException
import getpass
import paramiko
def getSSHConnection(hostName):
config = SSHConfig()
user = getpass.getuser()
config.parse(open('C:/Users/' + user +'/.ssh/config'))
host=config.lookup(hostName)
# setup SSH client
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Check for proxy settings
try:
print host ['proxycommand']
proxy = paramiko.ProxyCommand(host['proxycommand'])
except:
proxy = None
#Setup the SSH connection
try:
if (proxy is None):
client.connect(host['hostname'],22, username=host['user'],key_filename=host['identityfile'])
else:
client.connect(host['hostname'],22, username=host['user'], key_filename=host['identityfile'], sock=proxy)
except SSHException, ex:
print ex
return client
ssh_client = getSSHConnection('remoteA')
# run a command
print "\nRun a command"
cmd = 'ps aux'
stdin, stdout, stderr = ssh_client.exec_command(cmd)
print stdout.read()
ssh_client = getSSHConnection('remoteB')
# run a command
print "\nRun a command"
cmd = 'ps aux'
stdin, stdout, stderr = ssh_client.exec_command(cmd)
print stdout.read()
首先,當連接到remoteA它連接到remoteB崩潰的時候的作品完美,但隨後步驟:proxy = paramiko.ProxyCommand(host['proxycommand'])
。
File "C:\Python27\lib\site-packages\paramiko\client.py", line 291, in connect
for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed
在Cygwin,我可以使用命令ssh remoteB
連接,所以我知道該配置文件應該沒問題。
如果這有什麼差別,我在Windows 7上運行此
所以我設法繞過我的問題,而不是做端口轉發。 – jimmy