[英]Executing command from remote server into another remote server using Paramiko
I am trying to connect to a specific central server that has configured passwordless connection to all other servers, the server I am currently on has no access to the ones I want to run commands.我正在尝试连接到已配置与所有其他服务器的无密码连接的特定中央服务器,我当前所在的服务器无法访问我想要运行命令的服务器。 So I am trying to connect to central server and from there do
ssh
into other servers I need to run commands.所以我试图连接到中央服务器,然后从那里做
ssh
到我需要运行命令的其他服务器。 When I run this after I execute ssh
command program gets frozen and does not allow to execute commands into final remote server.当我在执行
ssh
命令后运行它时,程序被冻结并且不允许将命令执行到最终的远程服务器中。 In this case let's say I want to run ifconfig
on final server 'host.name'.在这种情况下,假设我想在最终服务器“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()
In general to execute commands on server to which you can connect only via another SSH server, you should use port forwarding:通常,要在只能通过另一台 SSH 服务器连接的服务器上执行命令,您应该使用端口转发:
Nested SSH using Python Paramiko嵌套 SSH 使用 Python Paramiko
But if I understood correctly, you do not want to authenticate from the local machine, as you have the authentication set up from the jump host (do you have a private key stored on the jump host?) In that case, you want this:但是,如果我理解正确,您不想从本地计算机进行身份验证,因为您已经从跳转主机设置了身份验证(您是否在跳转主机上存储了私钥?)在这种情况下,您想要这样:
Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko .在 Python Paramiko 的 SSH 服务器上的辅助 shell/命令中执行(子)命令。 Or you can bring the key from the jump host to the local machine.
或者您可以将密钥从跳转主机带到本地机器。 See Nested SSH with RSA key file .
请参阅带有 RSA 密钥文件的嵌套 SSH 。
Though instead of feeding the commands to ssh
via a redirected input, you might also use ssh
command line as a more standardized interface:虽然不是通过重定向输入向
ssh
提供命令,但您也可以使用ssh
命令行作为更标准化的接口:
ssh destination command
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.