使用Paramiko从远程服务器执行命令到另一个远程服务器

Executing command from remote server into another remote server using Paramiko

我正在尝试连接到一个特定的中央服务器,该服务器配置了到所有其他服务器的无密码连接,我当前所在的服务器无权访问我想要 运行 命令的服务器。所以我正在尝试连接到中央服务器,然后从那里 ssh 进入我需要 运行 命令的其他服务器。当我在执行 ssh 命令后 运行 时,程序被冻结并且不允许对最终远程服务器执行命令。在这种情况下,假设我想在最终服务器 'host.name'.

上 运行 ifconfig
def get_host_info_linux(self,host,db_engine):
    #Create ssh client from paramiko library
    client = paramiko.SSHClient()

    try:
        # Connect to remote host
        #logger.info(username_pass)
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname='tunel host name', username=f'{db_engine}')
        #this command is to do ssh into the server I want to execute commands
        ssh_cmd = f'ssh {host.name}'

        ssh_std = client.exec_command(ssh_cmd)
        if (ssh_std[2].readlines() == []):+

            logger.debug(ssh_std[1].readlines()[0])

        else:
            logger.error(ssh_std[2].readlines())

      client.exe_command('ifconfig')

    except Exception as e:
        logging.error(e)
    finally:
        client.close()
  • 通常要在只能通过另一个 SSH 服务器连接到的服务器上执行命令,您应该使用端口转发:

  • 但是如果我没理解错的话,你不想从本地机器进行身份验证,因为你已经从跳转主机设置了身份验证(你有私钥吗存储在跳转主机上?) 在那种情况下,你想要这个:
    . Or you can bring the key from the jump host to the local machine. See .

  • 虽然不是通过重定向输入向 ssh 提供命令,您也可以使用 ssh 命令行作为更标准化的界面:

    ssh destination command