WinRM(Windows远程管理)介绍,Python远程操作Windows

WinRM(Windows远程管理)介绍,Python远程操作Windows

WinRM(Windows Remote Management)是Windows对WS-Management的实现,WinRM允许远程用户使用工具和脚本对Windows服务器进行管理并获取数据。Server2008 R2中默认开启该服务,从Server2012开始,该服务便集成在系统中默认开启,Win7默认安装此服务,但是默认为禁用状态,Win8,Win10默认开启。这种远程连接不会被客户端察觉到,也不会占用远程连接数。

1. winrm命令介绍,须以系统管理员身份执行

//快速在服务端运行winrm  
c:\> winrm quickconfig                    
  
//查看winrm的运行情况  
c:\> winrm e winrm/config/listener        
  
//查看winrm的配置  
c:\> winrm get winrm/config             
  
// 将service中的allowUnencrypted设置为true,允许未加密的通讯  
c:\> winrm set winrm/config/service @{AllowUnencrypted="true"}      
  
//将client中的基本身份验证设置为true,允许  
c:\> winrm set winrm/config/client/auth @{Basic="true"}             
  
// 将client中的allowUnencrypted设置为true,允许未加密的通讯  
c:\> winrm set winrm/config/client @{AllowUnencrypted="true"}      
  
// 设置主机信任的客户端地址,这里host1,2,3可以填你所在的客户端机器的ip或者主机名  
c:\> winrm set winrm/config/client @{TrustedHosts="host1, host2, host3"}  

2.pywinrm简单使用,使用命令 pip install pywinrm安装pywinrm

方法一:

# -*- coding:utf8 -*-
import winrm

ip = ''
username = ''
password = ''
shell = 'ipconfig /all'

s = winrm.Session(ip, auth=(username, password), transport='ntlm')  # 远程连接windows
r = s.run_ps(shell)  # 执行脚本
print(r.status_code)
print(str(r.std_out, 'big5'))

方法二:

# -*- coding: utf8 -*-
from winrm.protocol import Protocol

p = Protocol(
    endpoint='http://ip:5985/wsman',  # http 5985  https 5986
    transport='ntlm',
    username='',
    password='',
    server_cert_validation='ignore')
shell_id = p.open_shell()  # 打开shell
command_id = p.run_command(shell_id, 'ipconfig', ['/all'])  # 执行命令
std_out, std_err, status_code = p.get_command_output(shell_id, command_id)  # 获取返回结果
print(str(std_out, 'big5'))
p.cleanup_command(shell_id, command_id)
p.close_shell(shell_id)  # 关闭shell
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xuerba

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值