Skip to content Skip to sidebar Skip to footer

Python Ssh In To A Jumpserver And Then Ssh In To A Host From The Jumpserver To Execute A Command

I want SSH to a jumpserver. From the jumpserver, I want SSH to a host and execute a command: 'rpm -qa | grep package' and get the output. Once done, I want successfully logout of t

Solution 1:

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

This works best in bash, but you can subprocess, or whatever you prefer to run it through python.

Suppose the IPs are:

  • jumpserver IP is 151.121.121.121
  • finalserver IP is 10.1.2.3

Like follows

# 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'

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

Solution 2:

You can install jumpssh pypi using pip

pip install jumpssh

Post a Comment for "Python Ssh In To A Jumpserver And Then Ssh In To A Host From The Jumpserver To Execute A Command"