【问题标题】:Pywinrm and HTTPS ConnectionPywinrm 和 HTTPS 连接
【发布时间】:2020-03-06 16:15:59
【问题描述】:

有谁知道如何使用 pywinrm 创建到 Windows 服务器的 HTTPS 连接?
我自己试过了,但无法建立连接。

到目前为止我已经完成的步骤:

  1. 安装并导入pywinrm pip install pywinrm
  2. 生成的证书和密钥:openssl req -nodes -new -x509 -keyout key.pem -out server.pem
  3. 将 server.pem 转换为 server.cer openssl x509 -inform PEM -in server.pem -outform DER -out server.cer
  4. 在目标 Windows 服务器上安装了 server.cer(在“Trusted Root Cert. Auth”内)

注意事项:

  • WinRM 服务通过 HTTPS 工作,该服务已在另一台基于 Windows 的计算机上使用不同的证书进行了测试
  • 我正在使用 macOS Mojave,Python 3.7.6

现在,我无法建立winrm连接,脚本

import winrm

destination = 'https://10.0.0.1:5986'
username = 'user'
password = 'password'
cert_pem = 'crt-temp/server.pem'
cert_key_pem = 'crt-temp/key.pem'

session = winrm.Session(destination, 
                        auth=(username, password), 
                        transport='certificate', 
                        cert_pem=cert_pem, 
                        cert_key_pem=cert_key_pem)
result = session.run_ps('hostname')
print(result.std_out)

以及我收到的错误:

requests.exceptions.SSLError: HTTPSConnectionPool(host='10.0.0.1', port=5986): url: /wsman 超过最大重试次数(由 SSLError(SSLCertVerificationError( 1, '[SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败) : 无法获取本地颁发者证书 (_ssl.c:1076)')))

如何解决这个问题?


使用:https://github.com/diyan/pywinrm

【问题讨论】:

  • 使用session = winrm.Session(destination, auth=(username, password), transport='ntlm')部分解决

标签: python https


【解决方案1】:

这可能是自签名证书的问题,其中签名证书颁发机构不被视为受信任的颁发机构。您可以跳过服务器证书验证或将 CA 证书添加到客户端计算机上的受信任证书颁发机构。

跳过证书验证

import winrm

destination = 'https://10.0.0.1:5986'
username = 'user'
password = 'password'
cert_pem = 'crt-temp/server.pem'
cert_key_pem = 'crt-temp/key.pem'

session = winrm.Session(destination, 
                        auth=(username, password), 
                        transport='certificate', 
                        cert_pem=cert_pem, 
                        cert_key_pem=cert_key_pem,
                        # Skip validation below
                        server_cert_validation='ignore'
    )

result = session.run_ps('hostname')
print(result.std_out)

将 CA 证书添加到受信任的证书颁发机构

# For CentOS/RHEL
mv yourselfsignedcacert.pem /etc/pki/ca-trust/source/anchors/
update-ca-trust

【讨论】:

    猜你喜欢
    • 2016-05-15
    • 2017-09-14
    • 2017-12-31
    • 1970-01-01
    • 2016-09-20
    • 2017-11-09
    • 1970-01-01
    • 2011-01-09
    • 2013-04-10
    相关资源
    最近更新 更多