Show Me Your Python SysAdmin One-Liners!

Ah, the lazyweb. Today, I’m putting together content for a class I’m teaching on basic Linux administration, but during my meeting with a group of trainees to determine the scope of the course, they requested that I completely skip any coverage of “perl -e” one-liners, and show them the Python equivalents. Of course, I found this page, which has a few, but I figured I’d put out the call for more, just to get a good collection of ideas, and a higher-level idea of how people are using Python for system administration for ‘quick-n-dirty’ jobs. If I get a bunch of interesting ones, I’ll collect them all somewhere for easy reference (or add them to the wiki linked above?), so link this callout wherever pythonistas can be found.

Oddly enough, my experience with Python has me going in the completely opposite direction: I don’t write as many one-liners as I did with perl. If it’s not obvious to me how to do something with sed, awk, grep, find, xargs, and the “regular” tools, I write a Python script. I’ve tried remembering some things I used nasty Perl one-liners for, but I guess they were sufficiently nasty that I’ve forgotten them.

By the way, if you’re a sysadmin who writes their tools using Python, do consider giving a talk at this year’s PyWorks conference in November!

  • Benjamin (bpeterson)

    I don’t do Python one-liners. My bin, however, if filled with little useful Python programs.

  • sage

    I used to use perl. I use python. Ask for quick task you want to do, and I’ll try to write them in python. I don’t usually write “one line” in python, but rather small scripts (usually, qucik scripts becomes modules, which becomes module trees, and then frameworks… So I prefer small and quick scripts than one line python typed in shell. But anyway ask for small jobs, and I’ll see if want to write it in “one line python”…

  • Brian Luft

    These aren’t sysadmin specific, but maybe you find a few gems in David Beazley’s PyCon2008 presentation:

  • Graham Fawcett

    I wrote a little script called ‘pyline’ to help do one-liners in Python, particularly for jobs where grep and sed might be used.

    E.g. to tail an Apache log, printing the IP and URL (first and seventh words) for any line containing the word ‘banana’…

    tail -f access_log | pyline “‘banana’ in line and [words[0], words[6]]”

    I find it very handy. But I agree that in Python it’s usually better to write a script than to golf something into a one-liner.

  • Marius Gedminas

    “Quick-n-dirty” and “Python” do not mix. It’s a cultural thing.

  • Ville

    One of the mos useful things (it has saved us from many a samba/whatever configuration nightmare) is probably the one-line web server:

    python -c “import SimpleHTTPServer;SimpleHTTPServer.test()”

    Or, the one with port number that I’m too lazy to google at the moment.

  • Paddy3118

    Python one-liners?



    – Paddy.

  • Tim Golden

    @Ville: or, even simpler:

    python -m SimpleHTTPServer

  • Muharem Hrnjadovic

    Hi there, most sys admin jobs involve searching for files, grep’ing inside these and in-place search/replace operations.

    I have hence created a python module that facilitates these kinds of operations. See either or

  • Kumar McMillan

    PLEASE do not teach people to write one-liners in Python 🙂

    For basic linux administration I think you have to teach standard command line tools, there are so many useful ones. Off the top of my head I think of ps, du, df, find, mmv, vmstat, iostat, mpstat, lsof, tcpdump. that’s what you need for administration not Python.

    For example, why would you write something like this in Python when find does it so well?

    find . -name ‘*.pyc’ -exec rm {} \;

    However, for working with Python, I use a script ALL the time that basically does this (I call it pywhich) :

    print sys.argv[1], “:”
    mod = __import__(sys.argv[1])
    print mod.__file__
    if hasattr(mod, ‘__version__’):
    print mod.__version__

    something like that should be built into Python, IMO.

  • Pingback: Show Me Your Python SysAdmin One-Liners!

  • Jeff McNeil

    One-liners are a bit of a goofy animal in Python. That said, there’s one I’m guilty of all the time…

    python -c ‘import xmlrpclib; print xmlrpclib.Server(“http://localhost:8080”).methodName(a,b,c)’

    We run a lot of XMLRPC services, and the above serves as a fairly generic command line way to call each service.

  • Pingback: Recent Links Tagged With "xargs" - JabberTags

  • Vince

    How about running through a squid log with that crap epoch time stamp…this gives you the correct date and time and you can then get the useful information you need.

    cat access.log | python -c ‘while True:
    except EOFError:
    import time
    print y[0], y[1], y[4]’

    More sys admin one liners….MORE!!!!!!

  • Ted King

    If Perl’s “ucfirst” is not enough try this :
    cat raw.txt | python -c “import string,sys;[[sys.stdout.writelines(LT)] for LT in [[string.capwords(LS)+’\n’] for LS in sys.stdin.readlines()]]” > Proper.txt

    Keywords : capwords(), initcaps(), proper()
    SUSE 10, Python 2.4

  • DrRp

    Hi all,

    Sony Imageworks has released a python script for working on the command line…it lets you run all python string and list methods, and has a bunch of stuff overloaded for common tasks. It works just like awk or sed…you just pipe data in and then execute your python commands using some preset variables.

  • garder snake

    Try out using Ipython. It let’s you run shell commands natively. And ot is an interastive interpreter.