Firefox 16, a treat for developers http://t.co/cnd27CzT
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
can 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.
Like us on Facebook
This week Top Posts 
Top Things to do After Installing Ubuntu 13.04 ‘Raring Ringtail’ : Ubuntu 13.04 Raring Ringtail final is almost out. The final release it scheduled for release on Apri...0 comment(s) |
Install lamp with 1 command in Ubuntu 12.10, 13.04 Raring Ringtail & LinuxMint13 : Updated: 10/09/2012 :LAMP (Linux, Apache, MySQL and PHP) is an open source Web development platform ...0 comment(s) |
Howto: Upgrade to Ubuntu 13.04 Raring Ringtail from 12.04, 12,10 | Desktop & Server : Updated 05-04-2013: Ubuntu 13.04 Raring Ringtail will be released Soon, If you have ubuntu 12,10, 12...0 comment(s) |
Steganography- Hide Your Files Inside An Image in Linux : Nowadays, our personal computer is not only a work tool, it is also our private space where we sto...4 comment(s) |
How to use Remote Desktop in Ubuntu : Sometimes, we need to access our computer from other locations when we’re not at home and such. This...0 comment(s) |
Configure conky-Lua in Ubuntu (12.10 & 13.04 Raring Ringtail), Fedora, debian and LinuxMint | Howto Conky : Updated 05-04-2013: Conky is a free, light-weight system monitor for X, that displays any informatio...0 comment(s) |
Recent Posts
- Steganography- Hide Your Files Inside An Image in Linux
- Unix/Linux File Recognition. Did You Know?
- Migrate from MySQL to MariaDB in FreeBSD
- Connect Your Android Galaxy Tablet to Ubuntu via USB
- ElementaryOS Beta 1 and 2 Comparison and Review
- Introduction to the Linux Command Line
- A Secure Password
- Linux Kernel 3.10. It’s BIG!
- Monitoring Users Activity Using psacct or acct Tools in Linux
- Run Your Own Social Network Using elgg on RHEL / CentOS and Scientific Linux
Recent Comments



















Edson Carlos
| #
In debian no found. I need link download install in linux debian
jacky can
| #
You can actually unzip your secret archive by leaving out the -t option, instead using:
unzip newPhoto.jpg
DragonFartOutLoud
| #
it went to china.
DragonFartOutLoud
| #
great review! i’ve been using it as my main OS since Beta 1 release. its been a awesome ride so far :)
jet li
| #
i try the method, and i unzip the image contain zip secret, i dont find the file that i was hide, where it go? and how to open it? :s