【发布时间】:2020-03-06 16:15:59
【问题描述】:
有谁知道如何使用 pywinrm 创建到 Windows 服务器的 HTTPS 连接?
我自己试过了,但无法建立连接。
到目前为止我已经完成的步骤:
- 安装并导入pywinrm
pip install pywinrm - 生成的证书和密钥:
openssl req -nodes -new -x509 -keyout key.pem -out server.pem - 将 server.pem 转换为 server.cer
openssl x509 -inform PEM -in server.pem -outform DER -out server.cer - 在目标 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)')))
如何解决这个问题?
【问题讨论】:
-
使用
session = winrm.Session(destination, auth=(username, password), transport='ntlm')部分解决