Pywinrm和HTTPS连接

2024-05-29 07:31:28 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道如何使用pywinrm创建到Windows服务器的HTTPS连接吗
我自己尝试过,但是我无法建立连接

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

  1. 已安装和导入的pywinrm pip install pywinrm
  2. 生成的证书和密钥:openssl req -nodes -new -x509 -keyout key.pem -out server.pem
  3. 已将server.pem转换为server.ceropenssl x509 -inform PEM -in server.pem -outform DER -out server.cer
  4. 在目标Windows服务器上安装了server.cer(位于“受信任的根证书身份验证”内)

注意事项:

  • WinRM服务通过HTTPS工作,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): Max retries exceeded with url: /wsman (Caused by SSLError(SSLCertVerificationError( 1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)')))

如何对此进行故障排除?


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


Tags: keyhttpscertserverwindowspasswordcertificateout
1条回答
网友
1楼 · 发布于 2024-05-29 07:31:28

这可能是自签名证书的问题,其中签名证书颁发机构不被视为受信任的机构。您可以跳过服务器证书验证,也可以将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

相关问题 更多 >

    热门问题