Archive for category Programming

Review of “Python for Unix and Linux System Administration”

I first got introduced to Python a few years go. Between now and then I mostly did some hackish scripts solving various small problems that me or my colleagues had. It has not been until recently that I started writing something that can remotely be called serious stuff that often makes its way to production environment.

I recently got my hands on a copy of Python for Unix and Linux System Administration. After reading it, I felt the time I’ve invested in reading it was well spent. The author introduced the reader to many different situations where python would help make their lives as system administrators easier, without confusing the reader with some complex forms or statements. My feeling is that this book is aimed at people who want to use Python to solve their problems quickly and efficiently, but only have a limited experience with the language – and the books fits that purpose well with its rather superficial approach that the reader can later extend later on with various available resources. It would only be fair that I too mention some of the shortcomings that I noticed while reading this book.


  • The author introduces the reader to ways that Python can be used.
  • Most of the time there will be more than one way to accomplish a task. The author at times presents a scenario and showed the reader how to do the same task with different modules. This places the choice of which to use back where it belongs, with the reader.
  • The book has a website (most do these days) where the code examples can be downloaded.


  • More time was spent on iPython than was really needed.
  • The case of a word is important in Python. For instance “import Sys” and “import sys” are two completely different things. There were quite a few occasions where a module name was used as the first word in the sentence and because of that it was capitalized.
  • There was once instance that I saw where a script example had no indentation at all. Trying to run it would have resulted in complete failure.
  • It would have been nice if the script examples were named instead of leaving it to the reader to figure it out based on the imports used in another example.

When all is said and done I think I would recommend the book to others if I knew that they had at least some background with Python. And I would highly recommend that they check the addendum and errata pages.


The servers in our DMZ do not have any access to the outside world. Getting Symantec dat files onto the DMZ AV server was kind of a pain. I would manually download the latest file (when I remembered) and then copy it to the correct directory on the server. I was getting kind of tired of manually doing this and add to that it was only happening about 2 times a month. Today I decided that I would write a script in python to take care of getting the correct file and moving it to the right location for me.  I have not set it up as a scheduled task yet but I may in the future. Here is what I came up with:

Disclaimer: I know that this may not be the best code written. I am still learning so please make comments that could improve on what is here. Thanks.

#!/usr/bin/env python

import datetime
from ftplib import FTP
import shutil, os

### Config stuff ###
# FTP site to connect to
ftpsite = ''

# Directory where the files are stored on the FTP server
directory = 'AVDEFS/symantec_antivirus_corp/xdb/'

# Set a variable for todays date like Feb 03 to match to
today ="%b %d")

# Array to hold the directory listing of the remote server
data = []

# Set the directory on the DMZ AV server to copy to
destDir = r'\\SomeRemoteAvServer\RemoteDir'

def handleDownload(block):
    print ".",

# Find the file that matches todays date
def getFilename(data):
    for line in data:
        perms, x, y, z, size, month, day, time, filename = line.split()
        if len(day) == 1:
            day = '0' + day
        timestamp = month + ' ' + day
        if timestamp == today:
            return filename

def compareFileSize(filename,destFile):
    Compares src file to destFile to see if they are the same size.
    if os.path.getsize(filename) == os.path.getsize(destFile):
        return 1
        return 0

# Open a connection to the FTP server
ftp = FTP(ftpsite)

# Log in as anonymous
print 'Logging in.'
print ftp.login()

# Change to the directory where the files are stored

# Get a directory listing

# Get the file that matches todays date from the directory listing
filename = getFilename(data)

print 'Opening local file ' + filename
file = open(filename, 'wb')

# Download the file a chunk at a time
# Each chunk is sent to handleDownload
# We append the chunk to the file and then print a '.' for progress
# RETR is an FTP command
print 'Getting ' + filename
ftp.retrbinary('RETR ' + filename, handleDownload)

# Clean up time
# Close the local file
print 'Closing file ' + filename

# Disconnect from the FTP server
print 'Closing FTP connection'
print ftp.close()

destFile = os.path.join(destDir,filename)

# Now copy the file to the AV server in the DMZ if it does not already exist
# then remove it from the local system.
if os.path.isfile(destFile):
    print 'The file %s is already on the server.' % filename
    print 'Copying the file to %s' % destDir
    shutil.copy2(filename, destDir)
    # Compare the two files to make sure they copied correctly
    if compareFileSize(filename, destFile):
        print 'The file looks to have copied fine. Deleting the local copy now.'
        print 'Something seems to have gone wrong. The file did not copy correctly to %s' % destDir



Have you ever wanted to just have a list of the machines on your network? One that you could easily pass to a script / application? Well I sure have.  So I do the net view > outfile.txt then scrub the text file dance. All I really want is a list of the machine names. No \\ in front of the names. No description of the systems either. I want to go from this:

\\comp1                my computer
\\comp3                test machine

to this:


So today I decided to write a little crap script to do this for me in one step. I know it isn’t pretty but I don’t really care. I just want it to work. The good thing is that I can now use the same kind of code in some of my other scripts and I don’t even need it to write to a file at all.

#!/usr/bin/env python
import os

outfile = open(r'pyNetView.txt', 'w')

indata = os.popen('net view')

for line in indata:
    if line.startswith('\\'):
        line = line.split()
        clean = line[0][2:]
        outfile.write(clean + '\n')


Comments are welcome. Use it if you want.


Editra text editor

Thanks to this blog post I have started using Editra for my coding needs. It at version 0.3.38 which is its 14th alpha release at the time of this writing. Don’t let the alpha part fool you though. It is a very nice app that has some good features. It has syntax hilighting for 60+ languages and code completion built in for python code. It has a plugin manager for easily installing some extra features. For a list of the currently available plugins see here. The one thing that I would like to see is the addition of hg as a vcs in the Projects plugin.

If you decide to try it out please post any bugs that you find in the projects bug tracker. If you are on then drop by the projects channel at #editra.

If you are a Foresight Linux user then you can try out the editor by running:

sudo conary update


hg vs git

Which to use. I have been using hg for some small things for a little while and I really like how easy it is to use. The commands are easy enough to remember and they make sense with what it is that you are trying to do. That with the fact that it is really pretty fast make it a great tool to have. The only thing that I have found that is kind of annoying is that it complains with files over 10MB. Not sure where to change it to allow for files of this size.

A few of my friends have talked about git though. I have looked at the commands and they are really quite similar to the hg commands.

So I guess my question is what do you use and why. Is it worth it to switch to git from hg (or the other way even)?



Hello again SPE

It has been some time since I used SPE for python work. Since I have recently started doing coding I wanted to give SPE another chance. I must say that I really like it. The only issue with it that I continue to have is that there is no print option for the code. I from time to time like to print out a copy of the code that I am working on and to do that with SPE I have to open the code with another piece of software and print from it. I do like the automatic UML work that is done.


Really good Python books

Any one that talks to me will eventually hear me talk about Python (even though I am not very good at it yet). I recently came across a few really good books on the subject. If you are intersted, or know someone that might be, check them out at

Some other good Python books are:

  1. Dive into Python
  2. byteofpython
  3. Thinking in Python

There are plenty of books out there this is just some of the ones I like.