使用python& amp;从linux连接到ssl上的powershell pywinrm

时间:2017-04-18 15:26:31

标签: python linux powershell ssl

我试图让python远程PowerShell使用来自docker容器的pywinrm工作,但是碰到连接到远程主机的墙我无法在ssl工作的任何地方找到一个工作示例(并且不会在没有它的情况下使用它)但是下面是我从文档和我对python的基本理解拼凑而成的,希望有人可以看到我出错的地方

我已经检查过的事情

  • 我检查过我可以使用Enter-PSSession -ComputerName x.x.x.x -UseSSL -Credential $creds;
  • 通过其他Windows机器连接
  • 我尝试过同时使用python3& python2并在两个
  • 中得到相同的错误
  • 我已经尝试过示例here,即使我从Windows机器上安装了自签名证书(使用接受的答案here添加了由于没有server_cert_validation ='忽略'并且没有它,这并不令人惊讶。参数但有点奇怪
  • 我已经尝试过base64编码密码,因为文档提到它需要它,但从未在任何示例中使用它
  • 我尝试将域名/机器名称添加到用户名(不在域名btw上)

以下是python3尝试的详细信息。

版本

python3 --version
Python 3.4.2

pip --version
pip 9.0.1 from /usr/local/lib/python3.4/dist-packages (python 3.4)

pip show pywinrm 
Name: pywinrm
Version: 0.2.2
Summary: Python library for Windows Remote Management
Home-page: http://github.com/diyan/pywinrm/
Author: Alexey Diyan
Author-email: alexey.diyan@gmail.com
License: MIT license
Location: /usr/local/lib/python3.4/dist-packages
Requires: six, xmltodict, requests, requests-ntlm

Python代码(open_winrm.py)

#!/usr/bin/env python3

import winrm
import base64

p = winrm.protocol.Protocol(
        endpoint='https://<some_ip_here>:5986/wsman',
        transport='ssl',
        username='<some_username_here>',
        password='<some_password_here>',
        server_cert_validation='ignore')

shell_id = p.open_shell()
command_id = p.run_command(shell_id, 'ipconfig', ['/all'])
std_out, std_err, status_code = p.get_command_output(shell_id, command_id)
p.cleanup_command(shell_id, command_id)
p.close_shell(shell_id)

结果

File "./open_winrm.py", line 13, in <module>
    shell_id = p.open_shell()
File "/usr/local/lib/python3.4/dist-packages/winrm/protocol.py", line 132, in open_shell
    res = self.send_message(xmltodict.unparse(req))
File "/usr/local/lib/python3.4/dist-packages/winrm/protocol.py", line 207, in send_message
    return self.transport.send_message(message)
File "/usr/local/lib/python3.4/dist-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

Dockerfile

FROM node:7.7.3

ENV DEBCONF_NONINTERACTIVE_SEEN="true" \
    DEBIAN_FRONTEND="noninteractive"

RUN apt-get update && \
    apt-get -y upgrade && \
    apt-get -y autoremove && \
    apt-get clean

RUN apt-get install -y etherwake locales locales-all python3 && \
    curl https://bootstrap.pypa.io/get-pip.py | python3 && \
    python3 -m pip install pywinrm

1 个答案:

答案 0 :(得分:0)

最后我放弃了pywinrm,因为我永远无法连接并使用ruby和第一次尝试的winrm gem