我是新來使用WMI;試圖編寫一個通過WMI連接到遠程系統的應用程序。我需要這個應用程序來監視用戶登錄,當用戶註銷和用戶閒置。登錄時,結束應用程序在系統上啓動的進程。在註銷或閒置時,我希望它重新啓動服務。這樣我就希望能最大限度地利用機器,同時不會影響用戶。的Python 2.7,WMI意外COM錯誤連接到域系統-2147352567非域系統
我有下面的代碼,主要是試圖使用Python WMI v1.4.9:
import wmi
import os
import threading
import getpass
class Application():
def __init__(self):
self.getCredentials(self)
self.getHostnames(self)
self.makeConnection(self)
self.runProgram(self)
def makeConnection(self, parent):
parent.server = parent.hostlist.pop()
print "makeConnection"
parent.wmiConnection = wmi.WMI(parent.server, user=parent.username, password=parent.password)
#For proof of concept, I'm settling for a single system for now. I can write the threading later.
def runProgram(self,parent):
print "runProgram"
parent.wmiConnection.Win32_Process.Create(CommandLine="cmd")
def getCredentials(self, parent):
parent.domain = raw_input('Domain: ')
parent.username = raw_input('Username: ')
parent.password = getpass.getpass(stream=None)
def getHostnames(self, parent):
if os.path.exists("hosts.txt"):
parent.hostslistfile = open("hosts.txt", "r")
parent.hostlist = parent.hostslistfile.readlines()
parent.hostslistfile.close()
if __name__ == "__main__":
app = Application()
我卡試圖憑據傳遞到的makeConnection()。我知道服務器接受WMI查詢,因爲我可以在不指定用戶運行的makeConnection()=和密碼=從連接到所述域的系統;結果連接成功。我嘗試以域\用戶身份傳遞用戶,並且無法找到更適當地執行此操作的文檔。
我收到的實際錯誤如下:
Traceback (most recent call last):
File "C:\Users\Urist\Dropbox\code\foreman.py", line 35, in <module>
app = Application()
File "C:\Users\Urist\Dropbox\code\foreman.py", line 10, in __init__
self.makeConnection(self)
File "C:\Users\Urist\Dropbox\code\foreman.py", line 15, in makeConnection
parent.wmiConnection = wmi.WMI(parent.server, user=parent.username, password
=parent.password)
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 (-2147352567, 'Exception occurred.', (0,
u'SWbemLocator', u'The RPC server is unavailable. ', None, 0, -2147023174), Non
e)>
似乎有在WMI食譜一個錯字。然而,使用在開始WMI教程參考,我可以成功地創建通過執行遠程連接:
connection = wmi.WMI("HOSTNAME", user=r"domain\user", password="password")
我因爲然後修改的makeConnection()模塊,以執行以下操作:
def makeConnection(self, parent):
parent.passedUsername = "r\"" + parent.username + "\""
parent.server = parent.hostlist.pop()
#print parent.server, parent.passedUsername
parent.wmiConnection = wmi.WMI(parent.server, user=parent.passedUsername, password=parent.password)
不幸的是,儘管parent.passedUsername是準確的,但這不起作用並仍然返回原始錯誤。