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.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- Stunnel Security for Oracle
- SourceClear Open
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- SUSE LLC's SUSE Manager
- My +1 Sword of Productivity
- Managing Linux Using Puppet
- Google's SwiftShader Released
- Non-Linux FOSS: Caffeine!
- Parsing an RSS News Feed with a Bash Script
- Doing for User Space What We Did for Kernel Space
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