secureCRT运行命令脚本

  之前写过VBS的secureCRT脚本, 功能太少了,改成python的

# $language = "python"
# $interface = "1.0"

#-----------------------------------------------------------------------------------
import os
import time

g_szConfigFile = "D:/Desktop/FTP/python3/secureCRT/test/info.txt"
g_szCommandFile = "D:/Desktop/FTP/python3/secureCRT/test/cmd.txt"
g_szDesktopFolder = "D:/Desktop"

def connectFun(szIPAddr, szUserName = "username", szPassWd = "password"):

    szSSH2cmd = "/SSH2 /L %s /PASSWORD %s %s" % (szUserName, szPassWd, szIPAddr)
    szTelnetcmd = "/Telnet %s" % (szIPAddr)
    result = False
    try:
        crt.Session.ConnectInTab(szSSH2cmd)
        result = True
    except:
        try:
            crt.Session.ConnectInTab(szTelnetcmd)
            crt.Screen.WaitForString("name")
            crt.Screen.Send(szUserName + "\r")
            crt.Screen.WaitForString("ass")
            crt.Screen.Send(szPassWd + "\r")
            result = True
        except:
            crt.Dialog.MessageBox(szIPAddr + " Login Failed!", "connect Failed")
            result = False
    
    return result

def readInfo(szFile):
    #crt.Dialog.MessageBox(szFile)
    if not os.path.exists(szFile):
        crt.Dialog.MessageBox("Can not find file", "No this File")
        return False, []

    objFile = open(szFile,'r')
    return True, objFile.readlines()

def execCMD(szCommandFile):
    bResult, listCMD = readInfo(szCommandFile)
    if bResult:
        for cmd in listCMD:
            crt.Screen.Send(cmd + "\r")
            while crt.Screen.WaitForString("ore-", 3):
                crt.Screen.Send(" ")

            crt.Screen.Send("\r\r\r")
        crt.Session.Disconnect()

#-----------------------------------------------------------------------------------
crt.Screen.Synchronous = True

pathDesktop = g_szDesktopFolder

bResult, listInfo = readInfo(g_szConfigFile)
time = time.strftime("%Y/%m/%d/%H-%M", time.localtime())
if bResult:
    for info in listInfo:
        info = info.strip('\n')
        if connectFun(info): 
            crt.Session.LogFileName = pathDesktop + "/logfile/" + time + "-"+ info + ".txt"
            crt.Session.Log(True)
            execCMD(g_szCommandFile)
            crt.Session.Log(False)
    crt.Dialog.MessageBox("Done!", "Good Job")

crt.Screen.Synchronous = False
发布了31 篇原创文章 · 获赞 11 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/MrRight17/article/details/81664765