适用环境:
远程 windows环境,知道ip ,用户名,密码的前提下
通过自己的电脑Python 创建远程服务器,的计划任务
如 :定时开关机,运行脚本。。。。。。。。。。。想干嘛干嘛,前提你知道
windows 计划任务不是 AT 更新为 schtasks
schtasks 知识学习请看 :https://blog.youkuaiyun.com/qq_36530891/article/details/95948676
Python实现代码:
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 29 15:51:50 2019
@author: lpf
"""
import os
import re
def netUserCont(ip,name,pwd):
cmd=r"net use \\"+ip+r"\ipc$ "+pwd+" "+ r"/user:"+name
runCmd(cmd)
def copyFile(inpath,outpath,ip):
cmd="copy "+inpath+r" \\"+ip+r"\c$"+outpath
runCmd(cmd)
def schtasksCreate(ip,name,pwd,sc,st,tn,tr):
cmd=r"schtasks /create /S "+ip+r" /u "+name+r" /p "+pwd+r" /sc "+sc+r" /f "+r"/st "+st+r" /ru "+r""" ""\Administrator /RL HIGHEST /tr """+tr+r" /tn "+tn
#print(cmd)
runCmd(cmd)
def runschtasks(ip,name,pwd,tn):
cmd=r"schtasks /run /S "+ip+ r" /u "+name+r" /p "+pwd+r" /tn "+tn
runCmd(cmd)
def runCmd(cmd):
results=os.popen(cmd).readlines()
print(results)
return results
def checkOS(ip):
try :
cmd="ping -n 4 "+ip
results=os.popen(cmd).read()
results=re.search("TTL=.*",results,re.M|re.I).group()
ttl=re.compile(r'\d+').findall(results)
ttl=int(ttl[0])
if ttl >=20 and ttl<=80:
print("this is linux !")
return "linux"
else:
print("this is windows!")
return "windows"
except:
print("网络错误!")
return False
print(results)
def winDeploy(ip,name,pwd):
#建立连接
netUserCont(ip,name,pwd)
#copy采集程序
inpath=r"E:\pycode\getpms\66c221be-6ab2-ef53-1589-fe16877914e2.vbs"
outpath=r"\Windows\System32\66c221be-6ab2-ef53-1589-fe16877914e2.vbs"
copyFile(inpath,outpath,ip)
#创建计划任务
sc="ONCE" #执行频率
st="16:42:00" #执行时间
tn="test" #任务名称
tr=r"C:\Windows\System32\66c221be-6ab2-ef53-1589-fe16877914e2.vbs" #要执行的命令
schtasksCreate(ip,name,pwd,sc,st,tn,tr)
#启动任务
runschtasks(ip,name,pwd,tn)
if __name__ == '__main__':
with open("bat.txt","r") as f:
for line in f.readlines():
l=line.split(",")
ip=l[0]
name=l[1]
pwd=l[2]
results=checkOS(ip)
if results=="linux":
pass
elif results=="windows":
winDeploy(ip,name,pwd)
else :
break