温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python Tricks如何使用pywinrm远程控制Windows主机

发布时间:2020-07-21 17:48:05 来源:亿速云 阅读:186 作者:小猪 栏目:开发技术

小编这次要给大家分享的是Python Tricks如何使用pywinrm远程控制Windows主机,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

启用 WinRM 远程服务: winrm quickconfig

查看 WinRM 服务监听状态: winrm e winrm/config/listener

C:\Windows\system32>winrm e winrm/config/listener
Listener [Source="GPO"]
  Address = *
  Transport = HTTP
  Port = 5985
  Hostname
  Enabled = true
  URLPrefix = wsman
  CertificateThumbprint
  ListeningOn = 127.0.0.1, 169.254.52.7, xx.xx.xx.xx, ::1, fe80::3989:dd91:e6b3:6f41%15, fe80::fd01:a9fd:c410:3407%12

允许使用 Basic 认证方式: winrm set winrm/config/service/auth @{Basic="true"}

winrm set winrm/config/service/auth @{Basic="true"}
Auth
  Basic = true [Source="GPO"]
  Kerberos = true
  Negotiate = true
  Certificate = false
  CredSSP = false
  CbtHardeningLevel = Relaxed

允许 WinRM 使用非加密的连接: winrm set winrm/config/service @{AllowUnencrypted="true"}

方式二:bat 脚本

call winrm quickconfig -quiet
call winrm set winrm/config/service/auth @{Basic="true"}
call winrm set winrm/config/service @{AllowUnencrypted="true"}

方式三:组策略

定位到 计算机配置 -> 策略 -> 管理模板 -> Windows 组件 -> Windows 远程管理(WinRM) -> WinRM 服务

启用 允许通过 WinRM 进行远程服务器管理允许基本身份验证允许未加密通信

Python Tricks如何使用pywinrm远程控制Windows主机

建议同时启用服务与防火墙策略:

计算机配置-> 策略 -> Windows 设置 -> 安全设置 -> 系统服务 -> Windows Remote Management (WS-Management) ,启动模式为自动。

计算机配置-> 策略 -> Windows 设置 -> 安全设置 -> 高级安全 Windows 防火墙 -> 高级安全 Windows 防火墙 - XXX -> 入站规则 ,开放 5985(HTTP)和 5986(HTTPS)端口。

二、Python 使用 pywinrm 连接 WinRM 服务

安装 pywinrm 库: pip install pywinrm

执行 cmd 命令:

>>> import winrm
>>> session = winrm.Session('xx.xx.xx.xx', auth=('Administrator', 'admin_password'))
>>> cmd = session.run_cmd('ipconfig')
>>> cmd.std_out
b'\r\nWindows IP Configuration\r\n\r\n\r\nEthernet adapter \xd2\xd4\xcc\xab\xcd\xf8:\r\n\r\n  Connection-specific DNS Suffix . : example.com\r\n  Link-local IPv6 Address . . . . . : fe80::3989:dd91:e6b3:6f41%15\r\n  IPv4 Address. . . . . . . . . . . : xx.xx.xx.xx\r\n  Subnet Mask . . . . . . . . . . . : 255.255.255.0\r\n  Default Gateway . . . . . . . . . : 172.20.23.254\r\n\r\nEthernet adapter \xd2\xd4\xcc\xab\xcd\xf8 2:\r\n\r\n  Media State . . . . . . . . . . . : Media disconnected\r\n  Connection-specific DNS Suffix . : \r\n'

执行 Powershell 命令:

>>> import winrm
>>> session = winrm.Session('xx.xx.xx.xx', auth=('Administrator', 'admin_password'))
>>> ps = session.run_ps('Get-Disk')
>>> ps.std_out
b'\r\nNumber Friendly Name Serial Number          HealthStatus     OperationalStatus   Total Size Partition \r\n                                                       Style   \r\n------ ------------- -------------          ------------     -----------------   ---------- ----------\r\n0   ST500DM002...       Z3TFS1S3       Healthy       Online         465.76 GB MBR    \r\n\r\n\r\n'

看完这篇关于Python Tricks如何使用pywinrm远程控制Windows主机的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI