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!
I don’t do Python one-liners. My bin, however, if filled with little useful Python programs.
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”…
These aren’t sysadmin specific, but maybe you find a few gems in David Beazley’s PyCon2008 presentation:
http://www.dabeaz.com/generators/index.html
I wrote a little script called ‘pyline’ to help do one-liners in Python, particularly for jobs where grep and sed might be used.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/437932
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.
“Quick-n-dirty” and “Python” do not mix. It’s a cultural thing.
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.
Python one-liners?
NOOOOOOOOOoooooooooooooooo……………………
- Paddy.
@Ville: or, even simpler:
python -m SimpleHTTPServer
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 http://hrnjad.net/src/scriptutil/ or http://muharem.wordpress.com/2007/05/20/python-find-files-using-unix-shell-style-wildcards/
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.
[...] http://www.protocolostomy.com/2008/07/16/show-me-your-python-sysadmin-one-liners/ asks Hoosgot, [...]
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.
[...] >> xargs I left my shell in the sun and now it is all GUI Saved by Belldandy on Mon 29-9-2008 Musings of an Anonymous Geek » Blog Archive » Show Me Your Python… Saved by cdwarren on Sun 28-9-2008 Linux Series:find和xargs Saved by dorcar51 on Fri 26-9-2008 [...]