It is time for some python programming. I love automating stuff and this time I will show you guys how to connect to your machine via ssh using python and run command on it. It is not hard, but at the same time not very easy to do this in python, since there are many modules and libraries to do this task. Are you curious about the module we will use in this tutorial?
We will use pxssh class of the pexpect module in Python which is used to to take care of some of the automation needs, like ssh and ftp. Do you want to make your sysadmin lifestyle as easy as possible? Then, why don’t you focus on this tutorial and try to learn as much as possible? Ok ,ok, i got your attention now, so let me give you guys a simple description about pxssh. You can also get information about pxssh by typing help(pxssh) after importing it in a python interactive shell.
Pxssh is based on pexpect. It’s class extends pexpect.spawn to specialize setting up SSH connections. I use pxssh frequently for making ssh connections in python. Pxssh uses the shell prompt to synchronize output from the remote host. In order to make this more robust it sets the shell prompt to something more unique than just $ or #. This should work on most Borne/Bash or Csh style shells.
You can read more about pxssh here.
Now let the fun begin.
In the first step, we import everything we need and assign machine details to variables like shown in Figure 1.
Then we create a function which will use pxssh to create and start a connection with the ssh server.
Study carefully how we use try and except so we can handle errors. Read more about error handling in python here.
What is the next step?
The next step is to create a function which will send the command to the machine after the ssh connection between two machines is established.
Now close the file and save it. It is ready to run.