python winrm操作windows命令记录

windows在powershell下执行

winrm quickconfig

winrm e winrm/config/listener

winrm set winrm/config/service/auth @{ Basic="true"}

winrm set winrm/config/service @{ AllowUnencrypted="true"} winrm get winrm/config

python示例代码:

import winrm
import logging

# 日志设置
LOG_FORMAT = "%(asctime)s %(levelname)s %(filename)s %(lineno)d %(message)s"
logging.basicConfig(filename='localAD.log', level="INFO", format=LOG_FORMAT)
console_handler = logging.StreamHandler()
console_handler.setLevel('INFO')   #info以上才输出到控制台
fmt = '%(asctime)s %(levelname)s %(filename)s %(lineno)d %(message)s'
formatter = logging.Formatter(fmt)
console_handler.setFormatter(formatter)
logger = logging.getLogger('updateSecurity')
logger.setLevel('INFO')
logger.addHandler(console_handler)

IP = '192.168.10.xx'        # 服务器IP
USER = 'x x'
PWD = 'x x x x'              # 服务器管理员密码

# 测试的命令
CMD = [
    'type null>1.x x x',
    'type null>1.x x x'
]


def runcmd(ip, user, pwd, cmd_list):
    try:
        win = winrm.Session('http://' + ip + ':5985/wsman', auth=(user, pwd))
        for cmd in cmd_list:
            ret = win.run_cmd(cmd)
            if ret.status_code == 0:  # 调用成功
                # a = ret.std_out.decode()
                logger.info('执行命令【' + cmd + '】成功')
                # print(ret.std_err)
            else:
                # a = ret.std_err.decode()
                logger.info('执行命令【' + cmd + '】成功')
                # print(ret.std_err)
        return True
    except Exception as e:
        print(e)


if __name__ == '__main__':
    # 测试命令执行
    runcmd(IP, USER, PWD, CMD)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值