Python Tricks 使用 pywinrm 遠(yuǎn)程控制 Windows 主機的方法
WinRM 即 Windows Remote Management,是微軟對于WS-Management 遠(yuǎn)程管理協(xié)議的實現(xiàn)。
一、受控端配置 WinRM 服務(wù)
方式一:cmd 命令行(管理員)
啟用 WinRM 遠(yuǎn)程服務(wù): winrm quickconfig 查看 WinRM 服務(wù)監(jiān)聽狀態(tài): winrm e winrm/config/listenerC:Windowssystem32>winrm e winrm/config/listenerListener [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 -quietcall winrm set winrm/config/service/auth @{Basic='true'}call winrm set winrm/config/service @{AllowUnencrypted='true'}
方式三:組策略
定位到 計算機配置 -> 策略 -> 管理模板 -> Windows 組件 -> Windows 遠(yuǎn)程管理(WinRM) -> WinRM 服務(wù) 。
啟用 允許通過 WinRM 進(jìn)行遠(yuǎn)程服務(wù)器管理 、 允許基本身份驗證 、 允許未加密通信 。
建議同時啟用服務(wù)與防火墻策略:
計算機配置-> 策略 -> Windows 設(shè)置 -> 安全設(shè)置 -> 系統(tǒng)服務(wù) -> Windows Remote Management (WS-Management) ,啟動模式為自動。
計算機配置-> 策略 -> Windows 設(shè)置 -> 安全設(shè)置 -> 高級安全 Windows 防火墻 -> 高級安全 Windows 防火墻 - XXX -> 入站規(guī)則 ,開放 5985(HTTP)和 5986(HTTPS)端口。
二、Python 使用 pywinrm 連接 WinRM 服務(wù)
安裝 pywinrm 庫: 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_outb’rnWindows IP ConfigurationrnrnrnEthernet adapter xd2xd4xccxabxcdxf8:rnrn Connection-specific DNS Suffix . : example.comrn Link-local IPv6 Address . . . . . : fe80::3989:dd91:e6b3:6f41%15rn IPv4 Address. . . . . . . . . . . : xx.xx.xx.xxrn Subnet Mask . . . . . . . . . . . : 255.255.255.0rn Default Gateway . . . . . . . . . : 172.20.23.254rnrnEthernet adapter xd2xd4xccxabxcdxf8 2:rnrn Media State . . . . . . . . . . . : Media disconnectedrn Connection-specific DNS Suffix . : rn’
執(zhí)行 Powershell 命令:
>>> import winrm>>> session = winrm.Session(’xx.xx.xx.xx’, auth=(’Administrator’, ’admin_password’))>>> ps = session.run_ps(’Get-Disk’)>>> ps.std_outb’rnNumber Friendly Name Serial Number HealthStatus OperationalStatus Total Size Partition rn Style rn------ ------------- ------------- ------------ ----------------- ---------- ----------rn0 ST500DM002... Z3TFS1S3 Healthy Online 465.76 GB MBR rnrnrn’
到此這篇關(guān)于Python Tricks 使用 pywinrm 遠(yuǎn)程控制 Windows 主機的方法的文章就介紹到這了,更多相關(guān)Python Tricks遠(yuǎn)程控制 Windows 主機內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)2. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實現(xiàn)代碼3. 如何使用瀏覽器擴(kuò)展篡改網(wǎng)頁中的JS 文件4. JSP servlet實現(xiàn)文件上傳下載和刪除5. jsp中sitemesh修改tagRule技術(shù)分享6. Ajax實現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)7. 爬取今日頭條Ajax請求8. JavaWeb Servlet中url-pattern的使用9. ASP基礎(chǔ)知識VBScript基本元素講解10. jsp EL表達(dá)式詳解
