Python Tricks 使用 pywinrm 远程控制 Windows 主机的方法


Posted in Python onJuly 21, 2020

WinRM 即 Windows Remote Management,是微软对于WS-Management 远程管理协议的实现。

一、受控端配置 WinRM 服务

方式一:cmd 命令行(管理员)

  • 启用 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 主机的方法的文章就介绍到这了,更多相关Python Tricks远程控制 Windows 主机内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
跟老齐学Python之用while来循环
Oct 02 Python
以一段代码为实例快速入门Python2.7
Mar 31 Python
结合Python的SimpleHTTPServer源码来解析socket通信
Jun 27 Python
wx.CheckBox创建复选框控件并响应鼠标点击事件
Apr 25 Python
使用Scrapy爬取动态数据
Oct 21 Python
用Python编写一个高效的端口扫描器的方法
Dec 20 Python
Python中的元组介绍
Jan 28 Python
python利用跳板机ssh远程连接redis的方法
Feb 19 Python
python pycharm的安装及其使用
Oct 11 Python
Python基于requests库爬取网站信息
Mar 02 Python
使用jupyter Nodebook查看函数或方法的参数以及使用情况
Apr 14 Python
python ssh 执行shell命令的示例
Sep 29 Python
Python json格式化打印实现过程解析
Jul 21 #Python
基于python实现删除指定文件类型
Jul 21 #Python
python打开音乐文件的实例方法
Jul 21 #Python
Python读取yaml文件的详细教程
Jul 21 #Python
Python中bisect的用法及示例详解
Jul 20 #Python
python为什么要安装到c盘
Jul 20 #Python
python如何代码集体右移
Jul 20 #Python
You might like
在Mac上编译安装PHP7的开发环境
2015/07/28 PHP
thinkphp如何获取客户端IP
2015/11/03 PHP
谈谈你对Zend SAPIs(Zend SAPI Internals)的理解
2015/11/10 PHP
关于恒等于(===)和非恒等于(!==)
2007/08/20 Javascript
在iframe里的页面编写js,实现在父窗口上创建动画效果展开和收缩的div(不变动iframe父窗口代码)
2011/12/20 Javascript
Js放到HTML文件中的哪个位置有什么区别
2013/08/21 Javascript
网站内容禁止复制和粘贴、另存为的js代码
2014/02/26 Javascript
浅谈jquery回调函数callback的使用
2015/01/30 Javascript
js实现带关闭按钮始终显示在网页最底部工具条的方法
2015/03/02 Javascript
javascript背景时钟实现方法
2015/06/18 Javascript
JavaScript的React Web库的理念剖析及基础上手指南
2016/05/10 Javascript
详解BootStrap中Affix控件的使用及保持布局的美观的方法
2016/07/08 Javascript
jQuery旋转插件jqueryrotate用法详解
2016/10/13 Javascript
jQuery学习之DOM节点的插入方法总结
2017/01/22 Javascript
详解Vue的computed(计算属性)使用实例之TodoList
2017/08/07 Javascript
JS+HTML5 Canvas实现简单的写字板功能示例
2018/08/30 Javascript
Python实现好友全头像的拼接实例(推荐)
2017/06/24 Python
matplotlib中legend位置调整解析
2017/12/19 Python
利用pyinstaller将py文件打包为exe的方法
2018/05/14 Python
解决PyCharm同目录下导入模块会报错的问题
2018/10/13 Python
Python XML转Json之XML2Dict的使用方法
2019/01/15 Python
我用Python抓取了7000 多本电子书案例详解
2019/03/25 Python
Python闭包与装饰器原理及实例解析
2020/04/30 Python
PyQt5实现简单的计算器
2020/05/30 Python
Python测试框架:pytest学习笔记
2020/10/20 Python
Html5元素及基本语法详解
2016/08/02 HTML / CSS
丹尼尔惠灵顿手表天猫官方旗舰店:Daniel Wellington
2017/08/25 全球购物
Revolution Beauty美国官网:英国知名化妆品网站
2018/07/23 全球购物
Skyscanner新西兰:全球领先的旅游搜索网站
2019/08/26 全球购物
银行员工职业规划范文
2014/01/21 职场文书
应聘会计求职信
2014/06/11 职场文书
党员群众路线个人整改措施思想汇报
2014/10/12 职场文书
电影小兵张嘎观后感
2015/06/03 职场文书
教你用python实现12306余票查询
2021/06/30 Python
MySQL系列之十二 备份与恢复
2021/07/02 MySQL
海贼王十大潜力果实,路飞仅排第十,第一可毁世界(震震果实)
2022/03/18 日漫