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
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 |
- 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 |
- How To Show Username On Panel In Ubuntu 13.04/13.10 : By default usernames are not displayed on panel in Ubuntu 13.04 and 13.10, In this simple tutorial w...0 comments |
- cowsay And fortune Combined Together : Hi linux geeks! In this article I will teach you a nice trick you can perform in the terminal with ...0 comments |
- Install lamp with 1 command in Ubuntu 12.10, 13.04 Raring Ringtail & LinuxMint13 : Updated: 10/09/2012 :LAMP (Linux, Apache, MySQL and PHP) is an open source Web development platform ...0 comments |
- Setup DNS Server On openSUSE 13.1 : What is DNS Server? As you might know, DNS, Domain Name System, is a system that resolves Domain ...0 comments |
- cowsay And fortune Combined Together
- How To Show Username On Panel In Ubuntu 13.04/13.10
- Setup DNS Server On openSUSE 13.1
- TeamViewer 9 Final Has Been Released!
- Twelve Vulnerabilities Have Been Fixed In Ubuntu 12.04, Time To Update
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6
- Install LibreOffice 4.1.3 in Elementary OS ‘Luna’
- How To Install Simple Scan in Crunchbang ‘Waldorf’
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .