Newbie: freebsd admin scripting

Evan Klitzke evan at yelp.com
Thu Jul 19 01:31:35 CEST 2007


On 7/17/07, Muffin <Muffin at bottompost.trim> wrote:
> I am using python for the first time on Unix and on FBSD. I need a few
> pointer, tips or tricks on scripting on config files. Mostly admin type
> configs like the rc.conf file usually setup like:
>
> someiteam=somevalue        #sometype of description
>
> Where I would like to toggle the somevalue in an easy way. I realize I
> could parse it with reg expression but that sounds kinda complicated. Is
> there an easer way a Unix python way of dealing with these type of
> config files.
>
> I found a built in mod for parseconfig but it deal with .ini file styles
> (windows) that include a [section] header as well as uses
> someiteam=somevalue format. I believe it requires the header though.

I think you're referring to ConfigParser. This is the standard way to
do config files in Python, and it is very simple to use and work with,
so I would recommend using it. The documentation can be found at
http://docs.python.org/lib/module-ConfigParser.html

> Books or howtos regarding Unix admin scripting(python) would be great.
> Is this type of scripting mainly executing a Unix command and capturing
> and parsing its output like this:
>
> x=command.getoutput("ifconfig")
>
> and them parsing x for what I want or is there a name space I can call
> to get , for example, a list of all interfaces?
>
> I have mainly used python in windows using the com space or win32 api
> aside from the base python lib, on Unix I am lost on what to do or how
> things are generally done. Advise on how to "think" in Unix python terms
> if that makes any sense.

You should use the subprocess module for interacting with other
programs, although it can be a little burdensome to work with. Ideally
you'll have a module that implements the functionality that you need
to use directly, rather than trying to control other processes from
Python.

Also, you should keep a collection of the scripts you write and try to
look for common patterns that come up. If you're diligent with this
you can start writing your own sysadmin python toolkit to make your
job a lot easier.

-- 
Evan Klitzke <evan at yelp.com>



More information about the Python-list mailing list