为了访问远程主机,我们需要登录jumphost1,然后登录jumphost2。为此,我们尝试创建一个隧道,如下面的 python 脚本所示。
我此连接的主要目的是执行脚本脚本并将输出重定向到脚本所在的同一位置脚本位置是本地计算机,pyc 文件将在本地计算机中创建隧道并连接远程计算机。
添加信息:两个跳转主机均通过密码启用 sshkeygen。所以它会询问密码。
[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!')
Run Code Online (Sandbox Code Playgroud)
建议更改的当前错误,它现在要求我输入密码,输入密码时出现以下错误
# python pyc
Enter passphrase for key '/root/.ssh/id_rsa':
2017-05-14 23:44:34,322| ERROR | Secsh channel 0 open FAILED: …Run Code Online (Sandbox Code Playgroud)