python 把本地文件远放到远程服务器,并执行远程服务器上的bat文件

# 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+++++++++++++++++++")

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值