Added subprocess, error that variable is not defined
Question: My script gets arguments such as

    
    
    C:\> python lookup.py "sender-ip=10.10.10.10"
    

And would take sender-ip and find wmi information

Now I added a ping subprocess to pint the machine BEFORE attempting to get wmi
information and I get following error

    
    
    C:\> python lookup.py "sender-ip=10.10.10.10"
    Traceback (most recent call last):
      File "lookup.py", line 10, in <module>
        ["ping", "-n", "1", userIP],
    NameError: name 'userIP' is not defined
    

Then I tried to define userIP global variable at the beginning of the
beginning of the program, but I get error

    
    
    C:\> python lookup.py "sender-ip=10.10.10.10"
    Traceback (most recent call last):
      File "lookup.py", line 10, in <module>
        ["ping", "-n", "1", userIP],
    NameError: global name 'userIP' is not defined
    

Here is the program(without global declaration)

    
    
    # import statements
    import sys, wmi, subprocess
    
    # subprocess
    ping = subprocess.Popen(
        ["ping", "-n", "1", userIP],
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )
    
    # get the arguments and extract user's IP address
    argument = sys.argv[1]
    attr_map = dict(item.strip().split('=') for item in argument.split(','))
    userIP =  attr_map['sender-ip']
    print userIP
    
    # can we ping the user's IP address?
    out, error = ping.communicate()
    
    # if we cannot ping user's IP address then print error message and exit program
    if out.find("Reply from") == -1:
        print userIP, "is NOT pingable."
        sys.exit()
    
    # remaining lines will execute if we can ping user's IP address
    c = wmi.WMI(userIP)
    for os in c.Win32_OperatingSystem():
      print os.Caption
    


Answer: You don't need global variable here. Just assign value to userIP before using
it:

    
    
    # import statements
    import sys, wmi, subprocess
    
    # get the arguments and extract user's IP address
    argument = sys.argv[1]
    attr_map = dict(item.strip().split('=') for item in argument.split(','))
    userIP =  attr_map['sender-ip']
    print userIP
    
    # subprocess
    ping = subprocess.Popen(
        ["ping", "-n", "1", userIP],
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )
    
    # can we ping the user's IP address?
    out, error = ping.communicate()
    
    # if we cannot ping user's IP address then print error message and exit program
    if out.find("Reply from") == -1:
        print userIP, "is NOT pingable."
        sys.exit()
    
    # remaining lines will execute if we can ping user's IP address
    c = wmi.WMI(userIP)
    for os in c.Win32_OperatingSystem():
      print os.Caption
    

