Newbie: freebsd admin scripting

M

Muffin

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.

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.

Thx
 
E

Evan Klitzke

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.
 
W

Will Maier

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.

rc.conf is a shell script on BSD systems. The OP will need to write
his own parser to read it. Take a look at the wiki's list of
parsers[0], specifically shlex[1]. I haven't used it to parse things
like rc.conf, but I imagine it could be useful.

Nothing in the standard library handles interface configuration,
though there may be a package somewhere to help you with that. In
general, yes, you'll be writing your own scripts wrapping around
system commands. As Evan says, be smart and keep those scripts
around. As you figure things out, generalize your scripts and create
some useful classes, etc.

[0] http://wiki.python.org/moin/LanguageParsing
[1] http://www.python.org/doc/current/lib/module-shlex.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top