Host-Hopping Scripts in Python
I provide telecommute consulting for a company called Crisp Hughes Evans (CHE) in Asheville, North Carolina. They needed a way to download Web logs from Apache, Zope and Plone Web sites, which were developed internally by employees, and Squid Proxy servers, which were running behind firewalls on client LANs that CHE set up. Because CHE believes in minimizing the number of different technologies used, I needed to see if I could download the Web logs reliably without changing the computer environment. Several methods exist to download logs: NFS, SAMBA, HTTP, FTP and so on. But because every computer has SSH installed and running, I decided to test SSH with Expect and my favorite programming language, Python.
What is SSH? SSH is a program used to log in to remote computers and execute commands or to transfer files. SSH also is encrypted. You can find out more information about SSH at www.openSSH.org. I chose SSH because all the computers had SSH, and I was trying to prevent installing new services.
What is Expect? Expect is an interface to automate login sessions of your favorite service (telnet, FTP, SSH and so on). Expect executes commands for you as if it was an interactive login. It can do this because it handles both the input and output when executing the program. In effect, it handles the expected responses of the programs. For example, it could log in as you and change your password for your account.
I choose Expect because I needed to be able to manipulate files on the remote computers before the data was downloaded. Many programming languages have an Expect interface, including Tcl, Perl and Python. You can find out more information about Expect at expect.nist.gov.
What is Python? Python is a clean and easy to write programming language used for Web programming and system administration scripts. It also is the basis for the Zope Web server and can create executable binaries, among other things. You can find more information about Python at www.python.org and the Python Expect module at pexpect.sourceforge.net.
Python is the language of choice for all the programmers at CHE. We use it for creating Zope and Plone Web sites, writing Web scripts in Apache, creating single-file binary executable Windows programs and other system administration tasks.
After many years of programming in Perl and PHP, Python seemed to be the next natural step. I had been programming in Python for CHE for a few years, and I didn't want to switch suddenly to a new language. It also is surprisingly easy for new programmers to learn Python with the proper guidance.
Python is real object-oriented language with a powerful structure to its logic. It also has great exception error handling.
<lit>I love Python.
What were the advantages of using the Python/Expect/SSH combination?
I did not have to install or support any new technologies, because SSH and Python were installed on all of our computers.
I did not want to install any new technologies, because I was telecommuting and didn't want to involve my associates on the East Coast with any new technologies.
SSH solved many security concerns.
The flexibility of Expect allows us to use it for future projects.
The next thing to do was write the Python code. The best approach to writing programs is to break down the project into segments that can be reused for other projects in the future. Thus, the first step was to create a generic Python Module, and it had to:
Detect if a script (or any file) exists on a remote computer.
Transfer a script (or any file) to a remote computer.
Execute a script on the remote computer.
Download a file, based on the response of the executed script on the remote computer.
Connect to the remote computer using SSH, accept any keys, if necessary, and then login.
Listing 1 shows a generic Python module I wrote.
The next step was to create the Python script I wanted to execute on the remote computer (Listing 2).
Then, I needed to create the Python script that uses the Expect interface (Listing 3).
Lastly, I executed the Python/Expect script with python SSH_Example.py. Everything worked out fine.
Once the basic module was created, it became clear that small Python/Expect scripts easily could be written for many other purposes to administrate simple tasks on remote computers.
Conclusion: the SSH/Python/Expect combination is useful for downloading files from all the remote computers we have. It is a solid and reliable technology that will be used in future projects at CHE.
Mark Nielsen works as a consultant trying to help the world realize the benefits of Linux, Python, PostgreSQL, Apache and Zope. In his spare time, he is trying to solve Ramsey numbers and might attend the Linux Lunacy cruise this year.
|illusive networks' Deceptions Everywhere||Aug 29, 2016|
|Happy Birthday Linux||Aug 25, 2016|
|ContainerCon Vendors Offer Flexible Solutions for Managing All Your New Micro-VMs||Aug 24, 2016|
|Updates from LinuxCon and ContainerCon, Toronto, August 2016||Aug 23, 2016|
|NVMe over Fabrics Support Coming to the Linux 4.8 Kernel||Aug 22, 2016|
|What I Wish I’d Known When I Was an Embedded Linux Newbie||Aug 18, 2016|
- illusive networks' Deceptions Everywhere
- Happy Birthday Linux
- New Version of GParted
- What I Wish I’d Known When I Was an Embedded Linux Newbie
- All about printf
- ContainerCon Vendors Offer Flexible Solutions for Managing All Your New Micro-VMs
- Updates from LinuxCon and ContainerCon, Toronto, August 2016
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Blender for Visual Effects
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide