python将ssh连接到jumpserver,然后从jumpserver连接到主机以执行命令

python将ssh连接到jumpserver,然后从jumpserver连接到主机以执行命令,python,ssh,pexpect,Python,Ssh,Pexpect,我想要SSH连接到jumpserver。从jumpserver,我希望SSH连接到主机并执行命令:“rpm-qa | grep package”并获得输出。完成后,我希望首先成功注销主机,然后注销跳转服务器 以下是我迄今为止所做的尝试: Import pexpect options = '-q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no' child = pexpect

我想要SSH连接到jumpserver。从jumpserver,我希望SSH连接到主机并执行命令:“rpm-qa | grep package”并获得输出。完成后,我希望首先成功注销主机,然后注销跳转服务器

以下是我迄今为止所做的尝试:

Import pexpect

options = '-q -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oPubkeyAuthentication=no'

child = pexpect.spawn('ssh ssheth@jumpserver %s'%(options), timeout=None)
child.expect(['password: '])
child.sendline('hello123')
child.expect('#')
child.sendline('ssh ssheth@host "rpm -qa | grep -v watch | grep extractor"')
child.expect(['password: '])
child.sendline(password)
child.logfile = fout
child.expect(".*\$ ", timeout=None)
child.close()
这将引发以下异常:

Timeout exceeded in read_nonblocking().
<pexpect.spawn object at 0x7f007800d190>
version: 2.4 ($Revision: 516 $)
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'ssheth@jumpserver', '-q', '-oStrictHostKeyChecking=no', '-oUserKnownHostsFile=/dev/null', '-oPubkeyAuthentication=no']
searcher: searcher_re:
    0: re.compile("#")
buffer (last 100 chars): ssheth@jumpserver:~[ssheth@jumpserver ~]$ 
[ssheth@jumpserver ~]$ 
before (last 100 chars): ssheth@jumpserver:~[ssheth@jumpserver ~]$ 
[ssheth@jumpserver ~]$ 
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 27286
child_fd: 91
closed: False
timeout: 2
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
在read_nonblocking()中超过超时时间。 版本:2.4($修订版:516$) 命令:/usr/bin/ssh 参数:['/usr/bin/ssh','ssheth@jumpserver“,”-q“,”-oStrictHostKeyChecking=no“,”-oUserKnownHostsFile=/dev/null“,“-oPubkeyAuthentication=no”] 搜索者:搜索者\u re: 0:重新编译(“#”) 缓冲区(最后100个字符):ssheth@jumpserver:~[ssheth@jumpserver ~]$ [ssheth@jumpserver ~]$ 之前(最后100个字符):ssheth@jumpserver:~[ssheth@jumpserver ~]$ [ssheth@jumpserver ~]$ 之后: 匹配:无 匹配索引:无 退出状态:无 flag_eof:False pid:27286 儿童:91 关闭:错误 超时:2 分隔符: 日志文件:无 日志文件读取:无 日志文件发送:无 maxread:2000 忽略情况:错误 searchwindowsize:无 发送前延迟:0.05 延迟后关闭:0.1 延迟后终止:0.1
最简单的答案是打开一个转发到远程服务器的端口

这在bash中效果最好,但您可以使用子流程,或者任何您喜欢通过python运行它的方式

假设IP为:

  • jumpserver IP为151.121.121.121
  • 最终服务器IP为10.1.2.3
如下

# Open a local port 13000 that will map through jumpserver and 
# connect to finalserver on port 22
ssh -L 13000:10.1.2.3:22 username@151.121.121.121
# Then open an ssh session to localhost:13000 (which will forward to the
# finalserver.com and run your command)
ssh username@localhost -p 13000 'rpm -qa | grep package'
旁白:看看你的剧本,回忆过去的日子。Python有一些ssh包装器。与一起使用。

您可以使用
pip

pip install jumpssh

遗憾的是,当我使用本地端口转发时,无法解析finalserver的主机名/IP,因为它只能通过跳转服务器进行访问。@ssheth,很抱歉,最后一行SSH错误:(.修复,现在,它应该是有意义的。嗨,乔纳森,我感谢你的回答-谢谢你的回答。效果很好。以下是根据你的回答遵循的步骤:步骤1:ssh-L 13000:172.31.15.22:22ssheth@jumpserver步骤2:$sshssheth@localhost-p 13000我考虑的另一种方法是在最终的h上启用基于公钥的身份验证从跳转服务器运行ost,只需运行以下命令:sshssheth@jumpserver“嘘ssheth@finalhost“rpm-qa | grep-v手表| grep提取器;退出”太好了。有时候:13000可以工作,有时候需要-p。我会用-p更新。下一步你可能会检查一下以简化~/.ssh/config文件,
man ssh\u config
,了解详细信息。你可以在那里指定所有这些,然后键入“ssh remote\u server”或任何你想标记它的东西。@Jonathan-我的情况类似,但有一个extra要做的事情。我想将一个文件从本地传输到jumpserver,然后从jumpserver传输到主服务器。在将文件从jumpserver传输到主服务器的过程中,我遇到了类似的错误。您的解决方案将解决SSH部分,但我应该如何处理SCP?请提供任何线索。