Keeping os context possible? Re: Python a good thing for sysadmins ?
magnus at thinkware.se
Fri Sep 14 01:42:10 CEST 2001
I agree with the other people that Python is really nice and
useful for system administration, and I might add that you can
use it effectively in many different operating systems, and
with the generic access to things like files, directories, http,
ftp etc, you can often use the same program in different OSes.
One thing bugs me though:
Let's make a simple example. I've probably forgotten the
syntax, since it was more than a year ago that I did this,
but I used Python scripts that accessed a DB/2 database
through system calls and the Unix db2 command line program.
(Yes, I know, one could use a database interface, but
I didn't have the possibility, and I'm sure we can come up
with another case where I needed the system calls.)
At the unix prompt I'd write something like:
db2 connect to database XXXX user YYYY password ZZZZ
db2 select this from that
The naive translation to Python would be something like
os.system('db2 connect to database XXXX user YYYY password ZZZZ')
dbcall = os.popen('db2 select this from that')
result = dbcall.readlines()
This won't work though. Each call (system or popen) will
spawn a new process and the connection made in the first
call will disappear as that call returns and that process
The simple solution was to put it into one call, as in
result = os.popen('db2 connect to database XXXX user YYYY password ZZZZ
& db2 select this from that & db2 disconnect').readlines()
The disadvantage here was that it worked in Solaris, but
not in NT. So what I actually did was to make an os script
(just the three db2 lines shown above) that I saved as bat-
file, which I called with os.popen(). (It wasn't always the
same database or table as you might guess.)
It was still a bad solution though. I used the output from
one select to form another select and so on, and this solution
meant that I had to make a lot of connect/disconnect, which took
the majority of the execution time. I also had to save passwords
in plain text files.
A unix shell script or an NT cmd file would live in the os
context. It wouldn't spawn a new process for every command,
and thus it would have no problems with one command depending
on changes in the environment caused by a previous command.
Can I achieve this from inside python? To be honest, I haven't
even quite understood how to use stdin with popen. Could that
somehow be used to achieve this? (And by the way, isn't the manual
page on popen2 wrong? "Returns the file object (child_stdout,
child_stdin)" Or am I confused here?
a, b = os.popen2('ls -l')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 9] Bad file descriptor
Will give you the directory listing you expected.
To return to the subject: Is the solution some expect-like
thingie? And in that case, which, if I want it to work on
both Windows and Linux?
> Hi list,
> I'm new here and for one good reason: I don't know Python, and I'd like
> to know more :-)
> In fact I already used Python but that was way back before, when I just
> wanted to learn programming. In the mean time I've become a professional
> java programmer, but now I might need python for sysadmin tasks.
> I'd like to know if Python could be used for system administration. And
> if there are people out there who use it for that. I noted for instance
> that there were no SIG relating to this topic...
> Could it be used for example to launch tasks from the cron daemon ?
> Thanks in advance,
Magnus Lyckå | Älvans väg 99 | magnus at thinkware.se | tel: 070-582 80 65
Thinkware AB | 907 50 UMEÅ | www.thinkware.se | fax: 070-612 80 65
More information about the Python-list