def connect(self, eventContext, deviceId, hostname, creds, namespace="root\\cimv2"): self._deviceId = deviceId library.com_init_ctx(byref(self.ctx), eventContext) cred = library.cli_credentials_init(self.ctx) library.cli_credentials_set_conf(cred) library.cli_credentials_parse_string(cred, creds, CRED_SPECIFIED) library.dcom_client_init(self.ctx, cred) def inner(driver): flags = uint32_t() flags.value = 0 ctx = library.WBEM_ConnectServer_send( self.ctx, # com_ctx None, # parent_ctx hostname, # server namespace, # namespace None, # username None, # password None, # locale flags.value, # flags None, # authority None) # wbem_ctx yield deferred(ctx); driver.next() result = library.WBEM_ConnectServer_recv(ctx, None, byref(self.pWS)) WERR_CHECK(result, self._deviceId, "Connect") driver.finish(None) return drive(inner)
def fetchSome(self, timeoutMs=wbemTimeoutInfinite, chunkSize=10): assert self.pEnum def inner(driver): count = uint32_t() objs = (POINTER(WbemClassObject)*chunkSize)() ctx = library.IEnumWbemClassObject_SmartNext_send( self.pEnum, None, timeoutMs, chunkSize ) yield deferred(ctx); driver.next() result = library.IEnumWbemClassObject_SmartNext_recv( ctx, self.ctx, objs, byref(count) ) WERR_CHECK(result, self._deviceId, "Retrieve result data.") result = [] for i in range(count.value): result.append(wbemInstanceToPython(objs[i])) talloc_free(objs[i]) driver.finish(result) return drive(inner)
def connect(self, eventContext, deviceId, hostname, creds, namespace="root\\cimv2"): self._deviceId = deviceId library.com_init_ctx(byref(self.ctx), eventContext) cred = library.cli_credentials_init(self.ctx) library.cli_credentials_set_conf(cred) library.cli_credentials_parse_string(cred, creds, CRED_SPECIFIED) library.dcom_client_init(self.ctx, cred) def inner(driver): flags = uint32_t() flags.value = 0 ctx = library.WBEM_ConnectServer_send( self.ctx, # com_ctx None, # parent_ctx hostname, # server namespace, # namespace None, # username None, # password None, # locale flags.value, # flags None, # authority None) # wbem_ctx yield deferred(ctx) driver.next() result = library.WBEM_ConnectServer_recv(ctx, None, byref(self.pWS)) WERR_CHECK(result, self._deviceId, "Connect") driver.finish(None) return drive(inner)
def fetchSome(self, timeoutMs=wbemTimeoutInfinite, chunkSize=10): assert self.pEnum def inner(driver): count = uint32_t() objs = (POINTER(WbemClassObject) * chunkSize)() ctx = library.IEnumWbemClassObject_SmartNext_send( self.pEnum, None, timeoutMs, chunkSize) yield deferred(ctx) driver.next() result = library.IEnumWbemClassObject_SmartNext_recv( ctx, self.ctx, objs, byref(count)) WERR_CHECK(result, self._deviceId, "Retrieve result data.") result = [] for i in range(count.value): result.append(wbemInstanceToPython(objs[i])) talloc_free(objs[i]) driver.finish(result) return drive(inner)
"WQL", query, WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, None) yield deferred(qctx); driver.next() pEnum = POINTER(IEnumWbemClassObject)() result = library.IWbemServices_ExecNotificationQuery_recv( qctx, byref(pEnum)) WERR_CHECK(result, self._deviceId, "ExecNotificationQuery") driver.finish(QueryResult(self._deviceId, self.ctx, pEnum)) except Exception, ex: if pEnum: c = library.IUnknown_Release_send_f(pEnum, self.ctx) yield deferred(c); driver.next() result = library.IUnknown_Release_recv(self.ctx) WERR_CHECK(result, self._deviceId, "Release") log.exception(ex) raise return drive(inner) def __del__(self): self.close() def close(self): if self.ctx: talloc_free(self.ctx) self.ctx = None
class Query(object): def __init__(self): self.ctx = POINTER(com_context)() self.pWS = POINTER(IWbemServices)() self._deviceId = None def connect(self, eventContext, deviceId, hostname, creds, namespace="root\\cimv2"): self._deviceId = deviceId library.com_init_ctx(byref(self.ctx), eventContext) cred = library.cli_credentials_init(self.ctx) library.cli_credentials_set_conf(cred) library.cli_credentials_parse_string(cred, creds, CRED_SPECIFIED) library.dcom_client_init(self.ctx, cred) def inner(driver): flags = uint32_t() flags.value = 0 ctx = library.WBEM_ConnectServer_send( self.ctx, # com_ctx None, # parent_ctx hostname, # server namespace, # namespace None, # username None, # password None, # locale flags.value, # flags None, # authority None) # wbem_ctx yield deferred(ctx) driver.next() result = library.WBEM_ConnectServer_recv(ctx, None, byref(self.pWS)) WERR_CHECK(result, self._deviceId, "Connect") driver.finish(None) return drive(inner) def query(self, query): assert self.pWS def inner(driver): qctx = None try: qctx = library.IWbemServices_ExecQuery_send_f( self.pWS, self.ctx, "WQL", query, WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_ENSURE_LOCATABLE, None) yield deferred(qctx) driver.next() pEnum = POINTER(IEnumWbemClassObject)() result = library.IWbemServices_ExecQuery_recv( qctx, byref(pEnum)) WERR_CHECK(result, self._deviceId, "ExecQuery") ctx = library.IEnumWbemClassObject_Reset_send_f( pEnum, self.ctx) yield deferred(ctx) driver.next() result = library.IEnumWbemClassObject_Reset_recv(ctx) WERR_CHECK(result, self._deviceId, "Reset result of WMI query.") driver.finish(QueryResult(self._deviceId, self.ctx, pEnum)) except Exception, ex: log.exception(ex) raise return drive(inner)
self.pWS, self.ctx, "WQL", query, WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, None) yield deferred(qctx) driver.next() pEnum = POINTER(IEnumWbemClassObject)() result = library.IWbemServices_ExecNotificationQuery_recv( qctx, byref(pEnum)) WERR_CHECK(result, self._deviceId, "ExecNotificationQuery") driver.finish(QueryResult(self._deviceId, self.ctx, pEnum)) except Exception, ex: if pEnum: c = library.IUnknown_Release_send_f(pEnum, self.ctx) yield deferred(c) driver.next() result = library.IUnknown_Release_recv(self.ctx) WERR_CHECK(result, self._deviceId, "Release") log.exception(ex) raise return drive(inner) def __del__(self): self.close() def close(self): if self.ctx: talloc_free(self.ctx) self.ctx = None