2

I am building or trying to build a python script which check's a list of ip addresses (ips.txt) for a specific program using the wmi python module. However, no matter how I handle the exceptions on assets with no RPC service running the script stops running on an error. I am using python 2.7.5

Can I catch and pass the error's to proceed? Can I catch the error and print or return a note that the ip was not alive or rpc was not running?

Thank you in advance

Here is my code:

import wmi
list = open("ips.txt")

for line in list.readlines():
    asset = line.strip('\n')
    c = wmi.WMI(asset)
    try:
        for process in c.Win32_Process (name="SbClientManager.exe"):
            print asset, process.ProcessId, process.Name
    except Exception:
        pass

I have tried handling the exceptions in multiple way's to continue parsing my list, but the script continues to error out with the following:

    Traceback (most recent call last):
  File ".\check_service.py", line 12, in <module>
    c = wmi.WMI(asset)
  File "C:\Python27\lib\site-packages\wmi.py", line 1290, in connect
    handle_com_error ()
  File "C:\Python27\lib\site-packages\wmi.py", line 241, in handle_com_error
    raise klass (com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147023174, 'The RPC server is unavailable.', None, None)>

Ultimately, I am just trying to continue the script and catch the error. Maybe a note stating that IP was not responsive would be helpful. Here are the exceptions samples that I have tried:

except Exception:
    sys.exc_clear()

except:
    pass

except wmi.x_wmi, x:
    pass

1 Answer 1

3

The traceback you pasted says that the error is in the c = wmi.WMI(asset) line. You need to put that line inside the try block.

Like so:

import wmi
list = open("ips.txt")
bad_assets = []

for line in list.readlines():
    asset = line.strip('\n')
    try:
        c = wmi.WMI(asset)
        for process in c.Win32_Process (name="SbClientManager.exe"):
            print asset, process.ProcessId, process.Name
    except Exception:
        bad_assets.append(asset)

Also, trying to catch the right exception is recommended.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.