Python 2.7 使用本地帐户连接python winrm

Python 2.7 使用本地帐户连接python winrm,python-2.7,winrm,Python 2.7,Winrm,我正在尝试使用winrm在CentOS上通过python 2.7.13连接到Windows server 2012。服务器不是域的一部分。我创建了一个独立的本地管理员帐户来连接到它 使用winrm configSDDL default为该用户提供所有访问权限 已将我的客户端计算机添加到受信任的主机 PS C:\Windows\system32> Get-Item WSMan:\localhost\Client\TrustedHosts WSManConfig: Microsoft

我正在尝试使用winrm在CentOS上通过python 2.7.13连接到Windows server 2012。服务器不是域的一部分。我创建了一个独立的本地管理员帐户来连接到它

使用
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
>>> 
>s=winrm.Session('abc',auth=('abc\script runner','xxx'))
>>>r=s.run_cmd('ipconfig',['/all']))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/local/lib/python2.7/site packages/winrm/_init__.py”,第37行,在run\u cmd中
shell\u id=self.protocol.open\u shell()
open_shell中的文件“/usr/local/lib/python2.7/site packages/winrm/protocol.py”,第132行
res=自发送消息(xmltodict.unpasse(req))
文件“/usr/local/lib/python2.7/site packages/winrm/protocol.py”,第207行,在发送消息中
返回self.transport.send_消息(message)
文件“/usr/local/lib/python2.7/site packages/winrm/transport.py”,第190行,在发送消息中
引发InvalidCredentialsError(“指定的凭据被服务器拒绝”)
winrm.exceptions.InvalidCredentialsError:指定的凭据被服务器拒绝
>>> 
此外,我启用了失败/成功登录的审核,但我没有看到连接尝试失败或成功

请告诉我少了什么?我想通过本地用户连接

谢谢你的帮助

  • 确保目标windows计算机中的网络连接类型为“专用”,如果为“公用”,则不会配置winrm
  • 打开命令提示符并键入:

    winrm-qc
    winrm set winrm/config/service@{AllowUnencrypted=“true”}

  • 打开Powershell并键入:

    启用psremoting
    设置项目WSMan:\localhost\Client\TrustedHosts*
    (“*”用于所有主机,您可以指定所需的主机)

  • 在python脚本中:

    导入winrm
    会话('yourWindowsHost',auth=('yourLocalUser','PasswordInPlaintText'))
    r=s.run_cmd('ipconfig',['/all'])

  • 确保目标windows计算机中的网络连接类型为“专用”,如果为“公用”,则不会配置winrm
  • 打开命令提示符并键入:

    winrm-qc
    winrm set winrm/config/service@{AllowUnencrypted=“true”}

  • 打开Powershell并键入:

    启用psremoting
    设置项目WSMan:\localhost\Client\TrustedHosts*
    (“*”用于所有主机,您可以指定所需的主机)

  • 在python脚本中:

    导入winrm
    会话('yourWindowsHost',auth=('yourLocalUser','PasswordInPlaintText'))
    r=s.run_cmd('ipconfig',['/all'])


  • 经过数周的努力,我终于明白了为什么我的证书会被拒绝。我从域中删除,确保它是私有的,并遵循这些步骤,然后我可以在ansible中成功运行win_ping模块。非常感谢。经过数周的努力,我终于明白了为什么我的证书会被拒绝。我从域中删除,确保它是私有的,并遵循这些步骤,然后我可以在ansible中成功运行win_ping模块。非常感谢。