[Tutor] Opsware Global Shell Scripting

Sander Sweers sander.sweers at gmail.com
Wed Jan 7 23:09:09 CET 2009


On Wed, Jan 7, 2009 at 22:04, Kayvan Sarikhani <ksarikhani at gmail.com> wrote:
> #!/usr/bin/python
> import os, sys
> sys.stdout = open('timecheck.txt','w')
> for servername in os.listdir('/opsw/Server/@'):
>     print '---', servername
>     os.system('rosh -n $SERVER_NAME -l $LOGNAME')
>     os.system('date')
> sys.stdout.close()
>
> The problem is this...for logging into systems via the global shell, one has
> to change directories to the one with the list of all managed servers (or
> specify the path for each one), and then launch a "rosh -n <hostname> -l
> <username>" to be able to get into it.
>
> This is of course not a Python error, but as you might guess from looking at
> the script, the whole $SERVER_NAME piece is probably wrong.

Indeed the variables are wrong

> I thought maybe
> I could do something like this...
>
> os.system('rosh -n', servername, '-l $LOGNAME')
>
> But of course os.system allows exactly one argument, and not three.

Almost, look at the below session in idle. The %s are replaced by the
variables servername and logname. See string formatting [1] for more
info.

>>> servername = 'testserver'
>>> logname = 'logname'
>>> print 'rosh -m %s -l %s' % (servername, logname)
rosh -m testserver -l logname

So you need to use something like below in your loop.

roshCommand = 'rosh -m %s -l %s' % (servername, logname)

os.system(roshCommand)

I do not see the servername variable being assigned a value in your
script so the above will fail. I assume it will be something like

logname = '/var/log/somelogfile'

You might want to look at the subprocess module as this gives you a
lot more control but is also more complicated.

Greets
Sander

[1] http://docs.python.org/library/stdtypes.html#string-formatting-operations


More information about the Tutor mailing list