Use Python To SSH To Your Machine
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.
Like us on Facebook
We need your assistance to stay live
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- Install Power Commands 0.1.5 On Ubuntu 14.04/13.10/12.10/12.04 : Power Commands, power management application created by the Atareao team , this app helps users to...0 comments |
- Install Uget Download Manager In Ubuntu, Fedora, Debian : uGet is a very light-weight and powerful open source download manager. On March 4th, 2014 Uget Do...0 comments |
- Install Crashplan backup tool in Linux : CashPlan is an automated backup solution that either uses the CrashPlan Central server or any other ...0 comments |
- Download Elementary OS 'Luna' Official Wallpapers : Hi Guys, Am a great lover of Elementary OS, so I like to move almost everywhere with some its beaut...0 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- Install Uget Download Manager In Ubuntu, Fedora, Debian
- Download Elementary OS ‘Luna’ Official Wallpapers
- Install Power Commands 0.1.5 On Ubuntu 14.04/13.10/12.10/12.04
- 5 Android Apps that’s Going to Make Running Your Business Less Stressful
- Puppet agent Install Using Script
- Install Corosync and Pacemaker On CentOS 6.5
- Install Fail2Ban On Ubuntu Server 13.04/13.10
- How To Install Google Chrome in Fedora 20, 19, 18
- How to surf using Tor Browser Bundle
- New Games got Greenlit on Linux by Steam Today!
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .