[Tutor] os module question

Alan Gauld alan.gauld at btinternet.com
Mon Feb 2 18:01:01 CET 2015


On 02/02/15 13:46, maurice.horan at thomsonreuters.com wrote:
> This is my first post. I'm a sysadmin and I'm new enough to python so I'm trying to practice.

Welcome to the list.
It looks like you are running a Unix variant but it helps to tell us 
exactly which OS and Python version.

> I'm want to run a script that takes a server name from the command line and
 > takes that servername which queries an internal asset database.

> I thought I could use the os module to take servername and run a
 > command against it to query the database and then populate
> a variable.

You can, although you will also want the subprocess module to
run the shell commands. Thee are several functions that will execute 
external programs in the os module but they are all deprecated in favour 
of subprocess which is safer and more flexible.

> Basically I have trouble with two things:
> 1)      Get the server to take the hostname and use it to run a command against it

os.uname() will return a tuple.
The second item is the hostname

> 2)      Send a variable (as opposed to a string) to the function to print

myfunc('astring')   # call func with string

myvar = 'astring'   # assign string to a variable
myfunc(myvar)       # call func with variable

notice where the quotes go...and that there are no $ or % signs
needed to indicate variables, as per the shell/perl etc.

> #!/usr/bin/env python
>
> import sys
> import os
>
> hostname = sys.argv[1]
> no_of_args = len(sys.argv)
>
> # Colours for pass and fail
> class bcolours:
>          PASS = '\033[32m'
>          FAIL = '\033[31m'
>          ENDC = '\033[0m'
>
>          def disable(self):
>                  self.PASS = ''
>                  self.FAIL = ''
>                  self.ENDC = ''

I'm not sure if you realize what this does? It does not change the 
values defined above. It creates local values in a specific object instance.

I'll assume you know what the colour values are for your
own terminal...


> def print_red(string):
>          red_string = bcolours.FAIL + string + bcolours.ENDC
>          print(red_string)
>
> def print_green(string):
>          green_string = bcolours.PASS + string + bcolours.ENDC
>          print(green_string)

And I assume you tested these at the >>> prompt.

> if no_of_args != 2:
>          print_red("Incorrect number of arguments entered")
>          sys.exit(1)
>
> #hostname_ppid = os.system('queryhost', '-c patch', hostname)
> hostname_ppid1 = "queryhost -c patch 'hostname'"
> hostname_ppid = os.system(hostname_ppid1)

This why you need subprocess. os.system only returns the error code,
it will not let you access the command output.

Read the subprocess docs they give many examples to show how
to use it.

I suspect you want something like:

query = subprocess.Popen(['queryhost', '-c patch', hostname],
                           stdout=subvprocess.PIPE)
hostname_ppid = query.communicate()[0]

It looks (and is) complicated compared to os.system() but its more 
secure and offers a lot of flexibility. Its well worth the extra pain.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list