WinRM 即 Windows Remote Management蒋情,是微軟對(duì)于 WS-Management 遠(yuǎn)程管理協(xié)議的實(shí)現(xiàn)。
一暂刘、受控端配置 WinRM 服務(wù)
方式一:cmd 命令行(管理員)
- 啟用 WinRM 遠(yuǎn)程服務(wù):
winrm quickconfig
- 查看 WinRM 服務(wù)監(jiān)聽(tīng)狀態(tài):
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 認(rèn)證方式:
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"}
方式三:組策略
定位到計(jì)算機(jī)配置 -> 策略 -> 管理模板 -> Windows 組件 -> Windows 遠(yuǎn)程管理(WinRM) -> WinRM 服務(wù)如贷。
啟用允許通過(guò) WinRM 進(jìn)行遠(yuǎn)程服務(wù)器管理漾根、允許基本身份驗(yàn)證厚宰、允許未加密通信腌巾。
建議同時(shí)啟用服務(wù)與防火墻策略:
計(jì)算機(jī)配置 -> 策略 -> Windows 設(shè)置 -> 安全設(shè)置 -> 系統(tǒng)服務(wù) -> Windows Remote Management (WS-Management),啟動(dòng)模式為自動(dòng)铲觉。
計(jì)算機(jī)配置 -> 策略 -> Windows 設(shè)置 -> 安全設(shè)置 -> 高級(jí)安全 Windows 防火墻 -> 高級(jí)安全 Windows 防火墻 - XXX -> 入站規(guī)則澈蝙,開(kāi)放 5985(HTTP)和 5986(HTTPS)端口。
二撵幽、Python 使用 pywinrm 連接 WinRM 服務(wù)
安裝 pywinrm 庫(kù):pip install pywinrm
執(zhí)行 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'
執(zhí)行 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'