【问题标题】:PyThon: winrm [WinError 10061] No connection could be made because the target machine actively refused it'))PyThon:winrm [WinError 10061] 无法建立连接,因为目标机器主动拒绝它'))
【发布时间】:2021-11-24 11:40:06
【问题描述】:

我有下面的简单代码尝试连接一台 Windows 机器,我可以使用 Windows 的 rdp(远程桌面)连接该机器

但是在 python 中使用 pywinrm 我得到了以下错误

requests.exceptions.ConnectionError: HTTPConnectionPool(host='10.XX.XX.XX', port=5986): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001F44F92A6D0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

我有密码

import winrm
session = winrm.Session('10.XX.XX.XX', auth=('admin','xxxxxx'), transport='ntlm', server_cert_validation='ignore') ##works
is_shell=True 
while is_shell:
    is_dir_get_or_create = session.run_cmd(r'if exist "C:\Temp\" (echo yes) else (echo no && mkdir C:\Temp)') ##failed
    is_shell=False

请帮帮我

提前致谢

【问题讨论】:

  • 确保在您的系统上启用了远程管理。此外,如果它只有安全,那么 5986 才能工作,否则它将在 5985 上。
  • 是的,我也试过 5985 也是同样的错误

标签: python-3.x winrm


【解决方案1】:

我能够解决它,所以您需要在使用上面代码的 python 使用 winrm 连接它之前将 winrm quickconfigure 设置到目标主机中

所以你必须在目标主机中执行以下操作

C:/> winrm qc

WinRM is not set up to receive requests on this machine.
The following changes must be made:
 
Set the WinRM service type to delayed auto start.
 
Make these changes [y/n]? y
 
WinRM has been updated to receive requests.
 
WinRM service type changed successfully.
WinRM is not set up to allow remote access to this machine for management.
The following changes must be made:
 
Create a WinRM listener on HTTP://* to accept WS-Man requests to any IP on this machine.
Enable the WinRM firewall exception.
Configure LocalAccountTokenFilterPolicy to grant administrative rights remotely to local users.
 
Make these changes [y/n]? y
 
WinRM has been updated for remote management.
 
Created a WinRM listener on HTTP://* to accept WS-Man requests to any IP on this machine.
WinRM firewall exception enabled.
Configured LocalAccountTokenFilterPolicy to grant administrative rights remotely to local users.

您以后也可以禁用它

PS> Stop-Service winrm
PS> Set-Service -Name winrm -StartupType Disabled

解决了上面的错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 2020-07-07
    • 2021-01-04
    • 2020-06-26
    • 2021-11-10
    • 2018-12-01
    相关资源
    最近更新 更多