目的
需要在linux環(huán)境下對(duì)客戶windows環(huán)境安裝agent
環(huán)境準(zhǔn)備
安裝
安裝包下載WMI client (WMIC) for Linux
測(cè)試
1.通過(guò)wmic命令遠(yuǎn)程查詢信息
# 查看計(jì)算機(jī)信息
wmic -U administrator%kk123456 //192.168.1.108 "select * from Win32_ComputerSystem"
# 查看操作系統(tǒng)信息
wmic -U administrator%kk123456 //192.168.1.108 "Select * from Win32_OperatingSystem"
# 查看進(jìn)程信息
wmic -U administrator%kk123456 //192.168.1.108 "Select * from Win32_Process Where CommandLine like '%explorer%'"
# 查看進(jìn)程監(jiān)控信息
wmic -U administrator%kk123456 //192.168.1.108 "Select PrivateBytes from Win32_PerfFormattedData_PerfProc_Process"
2.通過(guò)winexe命令遠(yuǎn)程執(zhí)行命令酱虎、
winexe -U administrator%kk123456 //192.168.1.108 "cmd /c ipconfig"
功能實(shí)現(xiàn)
基本原理
1.通過(guò)winexe遠(yuǎn)程調(diào)用echo命令锹淌,在windows機(jī)器上創(chuàng)建vbs腳本
vbs腳本中遠(yuǎn)程下載安裝包圃阳、執(zhí)行安裝腳本
2.通過(guò)winexe遠(yuǎn)程調(diào)用wscript執(zhí)行vbs腳本
代碼實(shí)現(xiàn)
1.winexe_wrapper.py文件
代碼功能: 調(diào)用winexe命令
#encoding: utf-8
import os
import subprocess
import logging
import traceback
logger = logging.getLogger(__name__)
def _command(args): #執(zhí)行系統(tǒng)命令
_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 = '"http://{host}"'.format(host=self.host)
arguments.append(hostaddr)
return arguments
def execute(self, cmds):
credentials = self._make_credential_args() #拼接命令行中用戶名密碼參數(shù)
if type(cmds) != type([]):
cmds = [cmds]
arguments = [self.bin] + credentials + cmds #拼接命令行參數(shù)
return _command(' '.join(arguments)) #執(zhí)行命令
def winexe(host, username, password, cmd):
if os.name == 'nt':
import wmi
try:
client = wmi.WMI(computer=host, user=username, password=password)
return client.Win32_Process.Create(CommandLine=cmd)[1]
except wmi.x_access_denied as e:
logger.error(e)
logger.exception(traceback.format_exc())
except wmi.x_wmi as e:
logger.error(e)
logger.exception(traceback.format_exc())
except BaseException as e:
logger.error(e)
logger.exception(traceback.format_exc())
return -1
else:
client = WinexeWrapper(host=host, username=username, password=password)
return client.execute("'{0}'".format(cmd))[0]
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')
- install.py腳本
代碼功能: 拼接執(zhí)行創(chuàng)建vbs腳本拓劝,調(diào)用winexe接口遠(yuǎn)程執(zhí)行
#encoding: utf-8
from __future__ import with_statement
from winexe_wrapper import WinexeWrapper
if __name__ == '__main__':
host = '192.168.1.108' #win機(jī)器地址
username = 'administrator' #win機(jī)器用戶名
password = 'kk123456' #win機(jī)器密碼
client = WinexeWrapper(host=host, username=username, password=password) #創(chuàng)建WinexeWrapper對(duì)象
vbs_file = r'c:\install' #vbs文件
download_file_url = r'http://192.168.1.101/package.exe' #安裝包下載位置
install_file = r'c:\install.exe' #安裝包下載后保存文件
log_file = r'c:\install.log' #安裝日志輸出
contents = [
'Set Post = CreateObject("Msxml2.ServerXMLHTTP")', #創(chuàng)建vbs中HTTP(S)請(qǐng)求對(duì)象
'Set Shell = CreateObject("Wscript.Shell")', #創(chuàng)建vbs中命令行對(duì)象
'Set FSO = CreateObject("Scripting.FileSystemObject")', #創(chuàng)建vbs中文件系統(tǒng)對(duì)象
'Post.setOption 2, 13056', #設(shè)置HTTPS請(qǐng)求不檢查服務(wù)器端證書
'Post.Open "GET","{0}",0'.format(download_file_url), #請(qǐng)求下載URL
'Post.Send()', #發(fā)送HTTP請(qǐng)求
'Set aGet = CreateObject("ADODB.Stream")', #創(chuàng)建vbs中流對(duì)象
'aGet.Mode = 3', #設(shè)置流對(duì)象模式(讀黑竞、寫)
'aGet.Type = 1', #設(shè)置流對(duì)象類型(二進(jìn)制類型)
'aGet.Open()', #打開流對(duì)象
'aGet.Write(Post.responseBody)', #將HTTP(S)請(qǐng)求結(jié)果寫入流對(duì)象
'aGet.SaveToFile "{0}",2'.format(install_file), #保存流到安裝包
'wscript.sleep 5000', #休眠5s
'Shell.Run "{0}", 0, True'.format(install_file), #同步執(zhí)行安裝腳本
'wscript.sleep 1000',
'FSO.DeleteFile("{0}"), True'.format(install_file), #刪除下載的安裝包
'wscript.sleep 1000',
'FSO.DeleteFile("{0}"), True'.format(vbs_file), #刪除vbs腳本
]
cmds = []
cmds.append("echo Rem client install > {0}".format(vbs_file)) #重寫vbs文件,第一行寫入注釋
for content in contents:
cmds.append('echo {0} >> "{1}"'.format(content, vbs_file)) #追加方式寫入vbs文件
cmds.append('wscript.exe /E:vbs "{0}" > {1}'.format(vbs_file, log_file)) #執(zhí)行vbs腳本
print client.execute("'cmd /c {0}'".format(' & '.join(cmds))) #調(diào)用winexe命令
后續(xù)功能
1.對(duì)winc進(jìn)行封裝, 通過(guò)查詢執(zhí)行進(jìn)程是否存在判斷是否安裝成功
2.并行對(duì)不同機(jī)器進(jìn)行安裝