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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Ubuntu Online Summit
- Devuan Beta Release
- The Qt Company's Qt Start-Up
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- May 2016 Issue of Linux Journal
- The US Government and Open-Source Software
- Open-Source Project Secretly Funded by CIA
- The Death of RoboVM
- EnterpriseDB's EDB Postgres Advanced Server and EDB Postgres Enterprise Manager
- New Container Image Standard Promises More Portable Apps
In modern computer systems, privacy and security are mandatory. However, connections from the outside over public networks automatically imply risks. One easily available solution to avoid eavesdroppers’ attempts is SSH. But, its wide adoption during the past 21 years has made it a target for attackers, so hardening your system properly is a must.
Additionally, in highly regulated markets, you must comply with specific operational requirements, proving that you conform to standards and even that you have included new mandatory authentication methods, such as two-factor authentication. In this ebook, I discuss SSH and how to configure and manage it to guarantee that your network is safe, your data is secure and that you comply with relevant regulations.Get the Guide