[Tutor] os module question

maurice.horan at thomsonreuters.com maurice.horan at thomsonreuters.com
Mon Feb 2 14:46:29 CET 2015


Hi,

This is my first post. I'm a sysadmin and I'm new enough to python so I'm trying to practice.

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. If the variable is equal to none it produces an error message in red, if it's successful then it prints it in green. I left a comment in for the first.

Basically I have trouble with two things:

1)      Get the server to take the hostname and use it to run a command against it

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

Thanks for reading geveryone!
Maurice

#!/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 = ''

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)

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)

if hostname_ppid == "None":
        print_red("No PPID")
        sys.exit(1)
else:
        print_green(hostname_ppid)
        sys.exit(1)

________________________________

This e-mail is for the sole use of the intended recipient and contains information that may be privileged and/or confidential. If you are not an intended recipient, please notify the sender by return e-mail and delete this e-mail and any attachments. Certain required legal entity disclosures can be accessed on our website.<http://thomsonreuters.com/prof_disclosures/>


More information about the Tutor mailing list