Python for systems administrators – The basics

If you have ever worked as a systems administrator or are planning a career as a systems administrator you will understand that knowledge of programming python-logocan be a powerful tool. Programming can not only help you save time with your work, but it also helps you to understand how other programs work. Python is one of those languages which are widely used throughout *nix systems and so in many cases is shipped by default with the operating system.

Python is often referred to as a scripting language, but Python is much more powerful and although it is a high level language it can perform the most basic to complex tasks. Over the next few weeks I will be covering some ways which you can use python to help you perform certain tasks as an administrator. Please note though I will not cover the very basics, because there are many websites that explain it much better than I can and in a lot more detail. So for the most part I will presume that you have some experience working with Python.

 If you’re unsure where to start out, it’s a good idea to start out with something simple such as tutorials point (http://www.tutorialspoint.com/python), once you understand some of the basic statements and syntax you can move on the standard library documentation to understand some of the commonly used functions (http://docs.python.org/library). At this point you should be able to create basic scripts. Then finally once you’re comfortable with the basics and the standard library you can move onto more advanced object oriented programming.

I will be using Python 2.7 throughout these tutorials and running the code on a Red Hat system. You might be wondering why I would use Python 2.7 rather than 3.2. First off you will find in many cases that Python 2.7 is more widely supported with non standard modules than 3.2. Secondly most of the programming I will be showing you will be backwards compatible with Python 3.2 but for the most part I am just much more fluent with Python 2.7.

For today let’s start out with something simple like reading log files. Every administrator should keep track of what’s going on with their log files, but let’s face it a lot of us forget from time to time. So what better way to keep track of log files than to display them every time you login?

So starting out we will have python read the last 5 lines from 2 different log files. We will then split the contents up into two sections to make it easier to read and then we will run the code from the ~/.bashrc file, which will execute the code when we login. If you have used Unix systems much you will know that the ~ or tilde represents the home directory of the user that’s logged in.

Here is our code:

{codecitation}#!/usr/bin/python

log1 = open(‘/var/log/secure’).read()

log2 = open(‘/var/log/httpd/error_log’).read()

log1_lines = log1.split(‘n’)

log2_lines = log2.split(‘n’)

print “—- SSH Access Log —-“

for line in log1_lines[-6:-1]:

print line

print “—- Apache Error Log —-“

for line in log2_lines[-6:-1]:

print line{/codecitation}

 First we tell python to open up and read both log files. I have selected the SSH access log and the Apache error log, but you can select any one you prefer. Next we split the log files up into a list in which each item is separated by a new line character. Then we simply create two for statements to print out the sliced lines between -5 and -1.

 If you are unfamiliar with slicing, basically it just allows you to select the items in the list between certain ranges. For example a slice between 0:4 would display the first five items (as 0 counts as the first item), but because we are using negative digits that means we are creating a slice between the fifth last and the last item.

 Next we save the code to the ~ or home directory as log.py and we give it the permissions “chmod 755 log.py” so that we have permission to read and execute the file.

 Finally open up the ~/.bashrc file and enter the following line to the bottom of the file

 {codecitation}# Read log files

./log.py

 {/codecitation}

That’s all there is to it, as you can see below we can now see the last 5 items from our log file every time we login.