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.
Webinar: 8 Signs You’re Beyond Cron
11am CDT, April 29th
Join Linux Journal and Pat Cameron, Director of Automation Technology at HelpSystems, as they discuss the eight primary advantages of moving beyond cron job scheduling. In this webinar, you’ll learn about integrating cron with an enterprise scheduler.Join us!
|Play for Me, Jarvis||Apr 16, 2015|
|Drupageddon: SQL Injection, Database Abstraction and Hundreds of Thousands of Web Sites||Apr 15, 2015|
|Non-Linux FOSS: .NET?||Apr 13, 2015|
|Designing Foils with XFLR5||Apr 08, 2015|
|diff -u: What's New in Kernel Development||Apr 07, 2015|
- Drupageddon: SQL Injection, Database Abstraction and Hundreds of Thousands of Web Sites
- Play for Me, Jarvis
- Non-Linux FOSS: .NET?
- Designing Foils with XFLR5
- Not So Dynamic Updates
- Flexible Access Control with Squid Proxy
- New Products
- Users, Permissions and Multitenant Sites
- diff -u: What's New in Kernel Development