# coding=utf8
import wmi
import os
import openpyxl
import time
fileFor7 = '7.mnt'
fileFor8 = '8.mnt'
# 远程执行bat文件
def runBat(host, user, password):
conn = wmi.WMI(computer=str(host), user=user, password=password)
filename = "C:\\***\\***.bat" # 此文件在远程服务器上
cmd_callbat = r"cmd /c call %s" % filename
print(cmd_callbat)
id, value = conn.Win32_Process.Create(CommandLine=cmd_callbat) # 执行bat文件
print(id, value)
# 上传文件到对应的服务器
def uploadFile(host):
hostName = str(host['机器名'])
storeId = host['店号']
os.system('net use \\\\' + hostName + '\\C$ password /user:username')
if str(storeId).startswith('7'):
os.system('xcopy C:\\install-file\\demo\\' + fileFor7 + ' \\\\' + hostName + '\\c$\\***\\download')
print('xcopy C:\\install-file\\demo\\' + fileFor7 + ' \\\\' + hostName + '\\c$\\***\\download')
elif str(storeId).startswith('8'):
os.system('xcopy C:\\install-file\\demo\\' + fileFor8 + ' \\\\' + hostName + '\\c$\\***\\download')
print('xcopy C:\\install-file\\demo\\' + fileFor8 + ' \\\\' + hostName + '\\c$\\***\\download')
os.system('net use \\\\' + hostName + '\\C$ /delete')
print('net use \\\\' + hostName + '\\C$ /delete')
# 从表格获取服务器地址list
def getHost(hostFilePath):
workbook = openpyxl.load_workbook(hostFilePath)
sh = workbook['errorHost']
res = list(sh.rows)
title = [i.value for i in res[0]]
cases = []
for item in res[1:]:
it = [i.value for i in item]
case = dict(zip(title, it))
cases.append(case)
print('获取Host信息成功,共' + str(len(cases)) + '条数据!')
return cases
if __name__ == '__main__':
# hostList = getHost('Host.xlsx')
# for item in hostList:
# uploadFile(item)
# runBat(host=item['机器名'], user="username", password="password")
try:
runBat('10.113.90.76', 'username', 'password')
except Exception as e:
print(e)
time.sleep(20)
print("END+++++++++++++++++++")
python 把本地文件远放到远程服务器,并执行远程服务器上的bat文件
于 2023-02-24 15:12:28 首次发布