python - 如何在 Paramiko 中模拟我们自己的 ssh 二进制文件?
问题描述
我有一个命令,例如
ssh -S myAuthServer hostname
正在尝试创建一个代码片段,但我不确定如何实现该-S
部分我目前拥有的代码:
#!/usr/bin/python
import paramiko
from paramiko import SSHClient, SSHConfig, SSHException
paramiko.util.log_to_file("/tmp/script.log")
def getSSHConnection():
config = SSHConfig()
host='server1'
# setup SSH client
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Check for proxy settings
proxy = paramiko.ProxyCommand('ssh -S myAuthServer root@%s' % host)
print "proxy:", proxy
#Setup the SSH connection
try:
if (proxy is None):
client.connect(host, port=22, username='root')
else:
print "-> Using paramiko with proxy"
client.connect(host, port=22, username='root', sock=proxy)
except SSHException, ex:
print ex
if client:
stdin, stdout, stderr = client.exec_command('hostname')
tables=stdout.readlines()
print "stdin:", stdin
print "stdout:", stdout
print "stderr:", stderr
print "tables:", tables
return client
getSSHConnection()
我收到的错误是
INF [20181011-11:14:55.131] thr=1 paramiko.hostkeys: Unable to handle key of type 1024
DEB [20181011-11:14:55.161] thr=2 paramiko.transport: starting thread (client mode): 0x4f35fd0L
DEB [20181011-11:14:55.161] thr=2 paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.2.1
ERR [20181011-11:15:10.242] thr=2 paramiko.transport: Exception: Error reading SSH protocol banner
ERR [20181011-11:15:10.245] thr=2 paramiko.transport: Traceback (most recent call last):
ERR [20181011-11:15:10.246] thr=2 paramiko.transport: File "/Users/root/Library/Python/2.7/lib/python/site-packages/paramiko/transport.py", line 1805, in run
ERR [20181011-11:15:10.246] thr=2 paramiko.transport: self._check_banner()
ERR [20181011-11:15:10.246] thr=2 paramiko.transport: File "/Users/root/Library/Python/2.7/lib/python/site-packages/paramiko/transport.py", line 1957, in _check_banner
ERR [20181011-11:15:10.246] thr=2 paramiko.transport: 'Error reading SSH protocol banner' + str(e)
ERR [20181011-11:15:10.246] thr=2 paramiko.transport: SSHException: Error reading SSH protocol banner
ERR [20181011-11:15:10.246] thr=2 paramiko.transport:
解决方案
您不能将 Paramikooursshbinary
用作 SSH 实现。你甚至不能让它使用ssh
。
帕拉米科就是ssh
它自己。
它是一样的(正如你现在所知道的那样),就好像你想要ssh
使用一样oursshbinary
(这没有意义)。您必须让 Paramiko 做该做的事oursshbinary
。但我们不知道oursshbinary
相比ssh
.
回复您的问题的先前版本:
OpenSSH 客户端的-S
切换ssh
创建了一个连接共享,可以被其他实例ssh
或其他 OpenSSH 工具(如sftp
)重用。
Paramiko 不支持连接共享。它实际上这样做是没有意义的。
连接共享对工具/应用程序很有意义。工具的一个实例创建一个连接,相同或不同工具的其他实例可以重用该连接。
但是您在脚本中/编程时不需要它。你有你的 SSH 会话实例。您可以在整个脚本/程序中将其用于多种用途。换句话说,您可以共享您的实例。
具体来说,使用 SSH,OpenSSH 连接共享会为共享客户端打开单独的 SSH 通道。使用 Paramiko,您也可以通过同一个 SSH 连接打开多个单独的通道。这就是像Transport.open_sftp_client
(or SSHClient.open_sftp
), Transport.open_session
( SSHClient.invoke_shell
, SSHClient.exec_command
) 这样的方法。他们都在内部调用Transport.open_channel
. 对于单个 SSH 会话,您可以根据需要多次调用所有这些。
推荐阅读
- c++ - 如何将十六进制字节字符串转换为 uint8_t *array?
- java - [已解决]从 spring.start.io 创建基本示例后,请求映射返回 404 而不是映射的字符串
- android - TypeError: Network request failed only on POST method
- ios - 训练图像的纵横比是否会影响 Turi Create 创建的对象检测模型?
- recursion - DrRacket 计划合同违反预期数量
- python - Raspberry Pi 未按预期显示带有屏幕的 pygame
- python - ahtzee 上节得分
- matlab - 重写 medfilt1 MATLAB 函数以支持 codegen
- javascript - 无法在 Cheerio 中显示选择器内容
- python - 如何获取 Gmail API 消息的文本/纯文本部分