且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python ssh进入跳转服务器,然后从跳转服务器ssh进入主机以执行命令

更新时间:2023-12-05 14:18:34

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

Simplest answer is to open a port forwarding to the remote server.

这在 bash 中效果***,但您可以进行子处理,或者您喜欢通过 python 运行它的任何方式.

This works best in bash, but you can subprocess, or whatever you prefer to run it through 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 包装器.Paramiko面料.

Aside: looking at your expect script and remembering the old days. Python has some ssh wrappers. Paramiko is used with Fabric.