linux通过 WMI 对windows进行监控和管理 ,window不需要开启服务,Linux 安装wmic

目的 :

  • 通过 Linux 管理 windows 服务器 
  • 获取Windows相关的系统信息及配置
  • 获取cmd执行命令

知识点介绍:

  • wimc
    • WMIC扩展WMI(Windows Management Instrumentation,Windows管理工具) ,提供了从命令行接口和批命令脚本执行系统管理的支持。在WMIC出现之前,如果要管理WMI系统,必须使用一些专门的WMI应用,例如SMS,或者使用WMI的脚本编程API,或者使用象CIM Studio之类的工具。如果不熟悉C++之类的编程语言或VBScript之类的脚本语言,或者不掌握WMI名称空间的基本知识,要用WMI管理系统是很困难的。WMIC改变了这种情况。
    • 他可以查看本机的各种配置信息
    • 具体命令 使用方法请看 :https://www.cnblogs.com/yunsicai/articles/2986741.html  (参数 很详细)

wmic安装 :

  • contos 系统安装:
    • 网上大多数包失效不能直接下载
    • 需要安装包联系我
    • 安装方法和普通源码安装一样
    • 解压 -> /config -> make -> meke install
  • debian 系统安装:
    • root@qcore:~# dpkg -i libwmiclient1_1.3.14-3_amd64.deb
      root@qcore:~# dpkg -i wmi-client_1.3.14-3_amd64.deb

wmic测试:

  •  通过wmic命令远程查询信息
# 查看计算机信息
wmic -U administrator%kk123456 //192.168.1.108 "select * from Win32_ComputerSystem"

# 查看操作系统信息
wmic -U administrator%kk123456 //192.168.1.108 "Select * from Win32_OperatingSystem"

# 查看进程信息
wmic -U administrator%kk123456 //192.168.1.108 "Select * from Win32_Process Where CommandLine like '%explorer%'"

# 查看进程监控信息
wmic -U administrator%kk123456 //192.168.1.108 "Select PrivateBytes from Win32_PerfFormattedData_PerfProc_Process"
  • 通过winexe命令远程执行命令
  • winexe -U administrator%kk123456 //192.168.1.108 "cmd /c ipconfig"

     

python 的封装:

实例化 WinexeWrapper 类,实现的通过winexe 对windows 服务器执行cmd 命令

#encoding: utf-8

import os
import subprocess
import logging
import traceback


logger = logging.getLogger(__name__)


def _command(args):                                                             #执行系统命令
    _process = subprocess.Popen(args, \
                                stdin=subprocess.PIPE, \
                                stdout=subprocess.PIPE, \
                                stderr=subprocess.PIPE, \
                                shell=True)
    _stdout, _stderr = _process.communicate()
    _returncode = _process.returncode
    return _returncode, _stdout, _stderr


class WinexeWrapper(object):

    def __init__(self, host, username, password):
        self.username = username
        self.password = password
        self.host = host
        self.bin = '/bin/winexe'

    def _make_credential_args(self):
        arguments = []
        userpass = '--user="{username}%{password}"'.format(
            username=self.username,
            password=self.password,
        )
        arguments.append(userpass)
        hostaddr = '"//{host}"'.format(host=self.host)
        arguments.append(hostaddr)
        return arguments

    def execute(self, cmds):
        credentials = self._make_credential_args()                              #拼接命令行中用户名密码参数
        if type(cmds) != type([]):
            cmds = [cmds]
        arguments = [self.bin] + credentials + cmds                             #拼接命令行参数
        return _command(' '.join(arguments))                                    #执行命令

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    host = '192.168.1.108'
    username = 'administrator'
    password = 'kk123456'
    #print winexe(host, username, password, 'cmd /c ipconfig')   #错误 需要修改
    print winexe(host, username, password, r"'cmd /c ipconfig'")

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值