回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>要访问远程主机,我们需要先登录jumphost1,然后登录jumphost2。为此,我们尝试创建一个隧道,如下面的python脚本所示。在</p>
<p>这个连接的主要目的是执行脚本脚本并将输出重定向到脚本所在的同一位置
脚本位置是本地计算机,pyc文件将从这里创建隧道并连接远程计算机。在</p>
<p>添加信息:两个jumphost都是sshkeygen启用的密码短语。所以它会询问密码。在</p>
<pre><code>[root@centseven ~]# cat pyc
import paramiko
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
('1.5.18.1', 22),
ssh_username='user',
ssh_pkey="/root/.ssh/id_rsa",
ssh_private_key_password="userpass",
remote_bind_address=("1.15.18.1", 22),
local_bind_address=('127.0.0.1', 1111)
) as tunnel:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=127.0.0.1, port=1111, username=root, password=remotepass)
# do some operations with client session
stdin, stdout, stderr = client.exec_command("./script >> output.txt")
print stdout.channel.recv_exit_status() # status is 0
client.close()
print('FINISH!')
</code></pre>
<p><strong>当前错误,建议更改,它现在要求我输入密码,当输入密码时,会显示以下错误</strong></p>
^{pr2}$
<p><strong>编辑1</strong></p>
<pre><code>python stack.py
Enter passphrase for key '/root/.ssh/id_rsa': 2017-05-15 00:14:24,437| ERROR | Exception: Error reading SSH protocol banner
2017-05-15 00:14:24,439| ERROR | Traceback (most recent call last):
2017-05-15 00:14:24,439| ERROR | File "/root/.pyenv/versions/ansible2/lib/python2.7/site-packages/paramiko/transport.py", line 1740, in run
2017-05-15 00:14:24,440| ERROR | self._check_banner()
2017-05-15 00:14:24,440| ERROR | File "/root/.pyenv/versions/ansible2/lib/python2.7/site-packages/paramiko/transport.py", line 1888, in _check_banner
2017-05-15 00:14:24,440| ERROR | raise SSHException('Error reading SSH protocol banner' + str(e))
2017-05-15 00:14:24,440| ERROR | SSHException: Error reading SSH protocol banner
2017-05-15 00:14:24,440| ERROR |
2017-05-15 00:14:24,442| ERROR | Could not connect to gateway remotehost:22 : Error reading SSH protocol banner
Traceback (most recent call last):
File "stack.py", line 9, in <module>
remote_bind_address=("remotehost", 22)
File "/root/.pyenv/versions/ansible2/lib/python2.7/site-packages/sshtunnel.py", line 1482, in __enter__
self.start()
File "/root/.pyenv/versions/ansible2/lib/python2.7/site-packages/sshtunnel.py", line 1224, in start
reason='Could not establish session to SSH gateway')
File "/root/.pyenv/versions/ansible2/lib/python2.7/site-packages/sshtunnel.py", line 1036, in _raise
raise exception(reason)
sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway
</code></pre>
<p><strong>.ssh/config</strong></p>
<pre><code>## lo8
Host jump1-*
User user
IdentityFile ~/.ssh/id_rsa
ForwardAgent yes
ServerAliveInterval 60
ServerAliveCountMax 12
Host jump01-temporary
Hostname HostIP
Port 2222
Host jump02
Hostname HostIP
Port 2222
Host jump01
Hostname HostIP
Port 22
ProxyCommand ssh -W %h:%p jump01
Host jump02
Hostname HostIP
Port 22
ProxyCommand ssh -W %h:%p jump02
Host Remote host
Hotname HostIP
</code></pre>
<p>有两个跳转服务器,我们需要连接本地机器-->JUMP1-->Jump2-->Remte主机</p>