【问题标题】:Run commands on multiple SSH servers behind a gateway/jumpserver在网关/跳转服务器后面的多个 SSH 服务器上运行命令
【发布时间】:2021-07-08 19:49:19
【问题描述】:

我在连接到跳转服务器后尝试运行多个 SSH 会话。但是在第 10 个 IP 之后,我得到一个超时错误。我需要重新启动 SSH 会话吗?它不会超过 2.44.12.13 ip。

import socket

import paramiko
from jumpssh import SSHSession

#connect to jump server
ssh = paramiko.SSHClient()
gatewaysess = 
    SSHSession(host="105.111.156.15",username="user",password="Test").open()
print(gatewaysess.is_active())

list = ["8.40.12.11", "9.40.12.11", "9.40.12.18", "10.42.12.24", 
        "7.40.12.11", "5.40.12.9", "6.40.12.8", 
        "8.42.12.1", "7.42.12.8", "2.44.12.13", "1.44.12.38", "8.44.12.35",
        "2.40.12.99", "2.40.12.19", "3.40.12.20"]

for i in list:
    if i == "2.44.12.13":
        remoteses.close()
        remoteses.open()
        remoteses = gatewaysess.get_remote_session(
            i, username="user", password="test", look_for_keys=False)
        remoteses.is_active())

【问题讨论】:

    标签: python ssh network-programming paramiko


    【解决方案1】:

    最简单、最可靠(虽然不是最有效)的方法是将网关连接移到循环中。

    类似这样的东西(未经测试):

    list = ["8.40.12.11", "9.40.12.11","9.40.12.18","10.42.12.24", 
            "7.40.12.11","5.40.12.9","6.40.12.8", 
            "8.42.12.1","7.42.12.8","2.44.12.13","1.44.12.38","8.44.12.35","2.40.12.99",
            "2.40.12.19","3.40.12.20"]
    
    for i in list:
        with SSHSession(host="105.111.156.15",username="user",password="Test") as gatewaysess:
            gatewaysess.open()
            print(gatewaysess.is_active())
            with gatewaysess.get_remote_session(i, username="user", password="test", look_for_keys=False) as remoteses:
                print(remoteses.is_active())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-29
      • 2023-03-16
      • 1970-01-01
      • 2017-10-29
      • 2022-11-08
      • 1970-01-01
      • 2013-10-20
      • 2012-09-23
      相关资源
      最近更新 更多