2012-03-23 455 views
0

我是新来使用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是准确的,但这不起作用并仍然返回原始错误。

回答

1

它实际上并没有在凭证中导致错误的我nstead这是HOSTS.TXT文件,然后使用pop()方法以获得即返回分隔列表的主机名。该代码执行正确的,如果你的不是跟随:

def makeConnection(self, parent): 
    parent.server = parent.hostlist.pop().strip() 
    parent.wmiConnection = wmi.WMI(computer=parent.server, user=parent.username, password=parent.password) 
0

我不是100%肯定你正在与passedUsername =“r \””一块做什么。它看起来好像你要代表一个正常的字符串中的原始字符串。目前,忘记在R“域\用户”,只是做“域\用户” - 即翻倍反斜线,如果你需要,我也不能确定是否要编写被改写的用户名回父对象,但我。 “M假设你有一些很好的理由这样做

IIUC您的makeConnection应该是这个样子:。

def makeConnection(self, parent): 
    if parent.domain: 
     parent.passedUsername = "%s\\%s" % (parent.domain, parent.username) 
    else: 
     parent.passedUsername = parent.username 
    parent.server = parent.hostlist.pop() 
    parent.wmiConnection = wmi.WMI(
     parent.server, 
     user=parent.passedUsername, 
     password=parent.password 
    )