Python 实现远程登陆服务器部署文件,创建计划任务

本文介绍了一种使用Python远程管理Windows环境下计划任务的方法,包括定时开关机、运行脚本等操作。通过Python脚本实现远程连接、文件复制及计划任务的创建与启动,适用于需要自动化管理和调度的远程Windows环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

适用环境:

远程 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
                
                
            
    

运行效果:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值