2017-04-21 275 views
0

我試圖通過使用winrm的CentOS上的python 2.7.13連接到Windows server 2012。服務器不是域的一部分。我創建了一個單獨的本地管理員帳戶來連接它。使用本地帳戶連接python winrm

使用winrm configSDDL default向用戶提供所有訪問權限。

將我的客戶機添加到可信主機。

PS C:\Windows\system32> Get-Item WSMan:\localhost\Client\TrustedHosts 


    WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client 

Type   Name       SourceOfValue Value 
----   ----       ------------- ----- 
System.String TrustedHosts         172.27.150.95 

在防火牆中添加了異常,並確保服務已啓動。

PS C:\Windows\system32> netsh advfirewall firewall add rule name="WinRM-HTTP" dir=in localport=5985 protocol=TCP action=allow 
Ok. 

PS C:\Windows\system32> Get-Service -ComputerName abc -Name winrm | Select Status 

                                         Status 
                                         ------ 
                                         Running 


PS C:\Windows\system32> 

爲WinRM客戶端設置:

PS C:\Windows\system32> winrm get winrm/config/client 
Client 
    NetworkDelayms = 5000 
    URLPrefix = wsman 
    AllowUnencrypted = true 
    Auth 
     Basic = true 
     Digest = true 
     Kerberos = true 
     Negotiate = true 
     Certificate = true 
     CredSSP = false 
    DefaultPorts 
     HTTP = 5985 
     HTTPS = 5986 
    TrustedHosts = 172.27.150.95 

的Telnet工作:

xyz:~ # telnet 172.27.148.29 5985 
Trying 172.27.148.29... 
Connected to abc (172.27.148.29). 
Escape character is '^]'. 
^] 
telnet> quit 
Connection closed. 
xyz:~ # 

但還是:

>>> s = winrm.Session('abc', auth=('abc\script-runner', 'xxx'))     
>>> r = s.run_cmd('ipconfig', ['/all']) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/site-packages/winrm/__init__.py", line 37, in run_cmd 
    shell_id = self.protocol.open_shell() 
    File "/usr/local/lib/python2.7/site-packages/winrm/protocol.py", line 132, in open_shell 
    res = self.send_message(xmltodict.unparse(req)) 
    File "/usr/local/lib/python2.7/site-packages/winrm/protocol.py", line 207, in send_message 
    return self.transport.send_message(message) 
    File "/usr/local/lib/python2.7/site-packages/winrm/transport.py", line 190, in send_message 
    raise InvalidCredentialsError("the specified credentials were rejected by the server") 
winrm.exceptions.InvalidCredentialsError: the specified credentials were rejected by the server 
>>> 

而且,我啓用審計失敗/成功登錄但我沒有看到任何輝帶領或成功爲我的連接嘗試。

請告訴我缺少什麼?我想通過本地用戶連接。

感謝您的協助。

回答

0
  1. 請確保在目標Windows計算機上的網絡連接類型是「私人」,如果它是「公共」winrm不會被配置。
  2. 打開命令提示符,然後輸入:

    winrm qc winrm set winrm/config/service @{AllowUnencrypted="true"}

  3. 打開PowerShell和類型:

    enable-psremoting set-item WSMan:\localhost\Client\TrustedHosts * ( '*' 的所有主機,你可以指定你想要的主機)

  4. 在你的python腳本中:

    import winrm s = winrm.Session('yourWindowsHost', auth=('yourLocalAdmin', 'passwordInPlainText'), transport='ntlm') r = s.run_cmd('ipconfig', ['/all'])

相關問題