【问题标题】:python ssh in to a jumpserver and then ssh in to a host from the jumpserver to execute a commandpython ssh 进入跳转服务器,然后从跳转服务器 ssh 进入主机以执行命令
【发布时间】:2019-07-26 14:45:53
【问题描述】:

我想通过 SSH 连接到跳转服务器。从 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

【问题讨论】:

    标签: python ssh pexpect


    【解决方案1】:

    最简单的答案是打开一个端口转发到远程服务器。

    这在 bash 中效果最好,但你可以子进程,或者任何你喜欢通过 python 运行它。

    假设 IP 是:

    • 跳转服务器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 包装器。 ParamikoFabric 一起使用。

    【讨论】:

    • 很遗憾,当我使用本地端口转发时,最终服务器的主机名/IP无法解析,因为它只能通过跳转服务器访问。
    • @ssheth,抱歉,SSH 的最后一行是错误的 :(。已修复,现在应该可以理解了。
    • 嗨乔纳森,感谢您的回答 - 谢谢。奇迹般有效。以下是根据您的回复所遵循的步骤: 第 1 步:ssh -L 13000:172.31.15.22:22 ssheth@jumpserver 第 2 步:$ ssh ssheth@localhost -p 13000 我想到的另一种方法是启用基于公钥的身份验证在跳转服务器的最终主机上,只需运行以下命令: ssh ssheth@jumpserver "ssh ssheth@finalhost 'rpm -qa | grep -v watch | grep extractor; exit'"
    • 太棒了。有时 :13000 有效,有时需要 -p 。我将使用 -p 进行更新。您可能要检查以简化的下一件事是 ~/.ssh/config 文件,man ssh_config 以了解详细信息。您可以在此处指定所有这些,然后键入“ssh remote_server”或任何您想要标记的内容。
    • @Jonathan - 我的情况类似,但还有一件事要做。我想将文件从本地传输到 jumpserver,然后从 jumpserver 传输到主服务器。在从 jumpserver 到主服务器的 SCPing 文件期间,我遇到了类似的错误。您的解决方案将解决 SSH 部分,但我应该如何处理 SCP?请提供任何线索。
    【解决方案2】:

    您可以使用pip 安装jumpssh pypi

    pip install jumpssh
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      相关资源
      最近更新 更多