我正在尝试连接到已配置到所有其他服务器的无密码连接的特定中央服务器,我当前所在的服务器无权访问我要运行命令的服务器。因此,我尝试连接到中央服务器,然后从那里将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()
答案 0 :(得分:0)
通常,要在只能通过另一个SSH服务器连接到的服务器上执行命令,应使用端口转发:
Nested SSH using Python Paramiko
但是,如果我理解正确,您不希望从本地计算机进行身份验证,因为您已从跳转主机设置了身份验证(您是否在跳转主机上存储了私钥? )在这种情况下,您需要这样做:
Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko。或者,您可以将密钥从跳转主机带到本地计算机。参见Nested SSH with RSA key file。
尽管除了通过重定向输入将命令馈送到ssh
之外,您还可以使用ssh
命令行作为更标准化的界面:
ssh destination command