如何使用 Paramiko 访问远程主机? [英] How to access a remote host with Paramiko?

查看:43
本文介绍了如何使用 Paramiko 访问远程主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Paramiko 连接到另一台主机.我可以通过本地网络中的内部 IP 地址访问该主机,这工作正常(在 Python 中和通过控制台中的 ssh).

I am just getting started on using Paramiko to connect to another host. I can access this host via its internal IP address in my local network and this works fine (both in Python and via ssh in the console).

然而,当我尝试通过外部 IP 地址访问主机时,通过 Paramiko 访问失败,而控制台中的 ssh 仍然有效.这里唯一的区别是我使用机器的外部 IP 地址而不是主机的内部 IP 地址.

When I try to access the host via its external IP address, however, the access via Paramiko fails while ssh in the console still works. The only difference here is that instead of the host's internal IP address I use the machine's external one.

有人可以帮忙吗?难道Paramiko 会以某种方式与我在路由器上配置的端口转发发生冲突?

Could anyone please help? Could it be that Paramiko somehow conflicts with the port forwarding I have configured on our router?

这是我目前的代码:

import paramiko

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

target_host = 'external.IP'
# target_host = 'internal.IP'

# Internal access port for ssh transfer.
# target_port = 22

# External access port for ssh transfer.
target_port = ABCD

pwd = 'my.password'
un = 'my.username'

ssh.connect( hostname = target_host , username = un, password = pwd )

stdin, stdout, stderr = ssh.exec_command('ls -1 .')

print "STDOUT:\n%s\n\nSTDERR:\n%s\n" %( stdout.read(), stderr.read() )

添加:

收到的完整错误信息是:

The full error message received is:

回溯(最近一次调用最后一次):文件./test.py",第 25 行,在ssh.connect( 主机名 = target_host , 用户名 = un, 密码 = pwd )文件/usr/local/lib/python2.7/site-packages/paramiko/client.py",第 251 行,在连接中retry_on_signal(lambda: sock.connect(addr))文件/usr/local/lib/python2.7/site-packages/paramiko/util.py",第 270 行,在 retry_on_signal返回函数()文件/usr/local/lib/python2.7/site-packages/paramiko/client.py",第 251 行,在retry_on_signal(lambda: sock.connect(addr))文件/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",第224行,在meth返回 getattr(self._sock,name)(*args)socket.error: [Errno 61] 连接被拒绝

Traceback (most recent call last): File "./test.py", line 25, in ssh.connect( hostname = target_host , username = un, password = pwd ) File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 251, in connect retry_on_signal(lambda: sock.connect(addr)) File "/usr/local/lib/python2.7/site-packages/paramiko/util.py", line 270, in retry_on_signal return function() File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 251, in retry_on_signal(lambda: sock.connect(addr)) File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 61] Connection refused

Python 和控制台访问之间的唯一区别是端口.难道 Paramiko 总是通过端口 22 访问 ssh?如果是这样,有没有办法让Paramiko通过另一个端口?

The only difference between the Python and the console access is the port. Could it be Paramiko always goes via port 22 for ssh? If so, is there a way to direct Paramiko to go through another port?

推荐答案

您的脚本在我的主机上的输出,并进行了以下更改:stdin, stdout, stderr = ssh.exec_command('ls -1/root|head -n 5')

Output of your script on my host with this change: stdin, stdout, stderr = ssh.exec_command('ls -1 /root|head -n 5')

文件1.py:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
target_host = '127.0.0.1'
target_port = 22
pwd = 'password'
un = 'root'
ssh.connect( hostname = target_host , username = un, password = pwd )
stdin, stdout, stderr = ssh.exec_command('ls -1 /root|head -n 5')
print "STDOUT:\n%s\n\nSTDERR:\n%s\n" %( stdout.read(), stderr.read() )
# For Python3
# print("STDOUT:\n%s\n\nSTDERR:\n%s\n" %( stdout.read(), stderr.read() )) 

运行python 1.py:

STDOUT:
~
1
1421750672-TWya15.png
1.py
7


STDERR:

我认为你需要 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 避免.

I think you need ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) avoid.

The authenticity of host 'localhost (::1)' can't be
established.
RSA key fingerprint is 
22:fb:16:3c:24:7f:60:99:4f:f4:57:d6:d1:09:9e:28.
Are you sure you want to continue connecting 
(yes/no)? 

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
    paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='jesse', 
    password='lol')

使用 PARAMIKO 进行 SSH 编程 |完全不同

这篇关于如何使用 Paramiko 访问远程主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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