Host-Hopping Scripts in Python

How and why I used SSH, Python and Expect to transfer Web logs to a central computer for processing.

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 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

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 and the Python Expect module at

Why Python?
  • 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.

Listing 1. Generic Python Module

The next step was to create the Python script I wanted to execute on the remote computer (Listing 2).

Listing 2. Python Script for Remote Computer

Then, I needed to create the Python script that uses the Expect interface (Listing 3).

Listing 3. Python Script for Expect Interface

Lastly, I executed the Python/Expect script with python 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.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Perfection is realtive to

Mark N's picture

Perfection is realtive to the standards you require for perfection. Change the requirements, and it may no longer be perfect.


dudeabase's picture

i has a tabel

i will so date you

Anonymous's picture

i will so date you


Anonymous's picture

Serenity now!

use php instead

Martin Kuria's picture

is there an advantage of using python over php??

Scripts in Python

Anonymous's picture

hi Mark Nielsen

how are you, i would like some help from you regarding scripts in python as i am working in zope environment . i find difficult in writing scripts using python as i donot have good tutorial or document where i can refer me .could you please suggest some good pdf in python . where i can find useful information with easy learning and implementation


Try the "Dive Into Python"

Matt Taylor's picture

Try the "Dive Into Python" ebook available as PDF and also has a website. Was a huge assistance to me as a casual Python and Jython coder.

Need more opinion on PosgreSQL

Anonymous's picture

Hi Mark,

I see you advocate PostgreSQL, and I would like to hear your opinion/views on it.

May I suggest that you write another informative article for LinuxJournal on this interesting and relevant topic: "PostgreSQL".

(Note: I don't know how to do this without starting a database flame war/pissing contest, but I would like to hear from the people that really know PostgreSQL well.)