[Python] SecureCRT 自动Telenet连接设备并弹出窗口输入账号密码

功能:

编辑 "SessionList.txt" ,实现自动连接设备。

e.g:
SessionList.txt
192.168.11.161

192.168.11.162

特点: 利用类的方法与属性,弹出窗口输入账号密码。把正确的账号密码储存起来传递到后面的会话。

import os


SCRIPT_TAB = crt.GetScriptTab()
SCRIPT_TAB.Screen.Synchronous = True
SCRIPT_TAB.Screen.IgnoreEscape = True


class Account:
    def __init__(self):
        self.user = crt.Dialog.Prompt("Enter user name:", "Login", "", False)
        self.password = crt.Dialog.Prompt("Enter password:", "Login", "", True)

def CreateObjTab(user, password, session):
    objNewTab = crt.Session.ConnectInTab("/TELNET %s 23" % session)
    objNewTab.Screen.WaitForString("login:")
    objNewTab.Screen.Send(user + "\r")
    objNewTab.Screen.WaitForString("Password:")
    objNewTab.Screen.Send(password + "\r")
    while not objNewTab.Screen.WaitForString(">", 3):
        user = crt.Dialog.Prompt("Enter user name:", "Login", "", False)
        password = crt.Dialog.Prompt("Enter password:", "Login", "", True)
        objNewTab.Screen.WaitForString("login:")
        objNewTab.Screen.Send(user + "\r")
        objNewTab.Screen.WaitForString("Password:")
        objNewTab.Screen.Send(password + "\r")
    objNewTab.Screen.Send("enable\r")
    objNewTab.Screen.WaitForString("Password:")
    objNewTab.Screen.Send("123\r")
    objNewTab.Screen.WaitForString("#")
    return user, password

def AutoConnectTab(file):  ###Auto connect sessions from a txt
    ####This is to Open the "SessionList.txt" and get a list of the ip address
    if not os.path.exists(file):
        return
    sessionFile = open(file, "r")
    sessionArray = []
    for line in sessionFile:
        session = line.strip()
        if session:
            sessionArray.append(session)
    sessionFile.close()

    # Receive variable
    account = Account()
    account.user, account.password =CreateObjTab(account.user, account.password, sessionArray[0])

    for session in sessionArray[1:]:
        try:
            CreateObjTab(account.user, account.password, session)
        except ScriptError:
            pass
        if not SCRIPT_TAB.Session.Connected:
            return

AutoConnectTab(os.getcwd() + "\SessionList.txt")

另外用python写了一个bat并设置运行py的时候,自动运行这个bat,并返回值,后面可以做一些文本的操作等。(亲测有效)

getpath = os.path.dirname(os.getcwd())
###Create a "auto-crt.bat"

batfile = os.path.join(getpath, "auto-crt.bat")

def main():
    getbat(batfile)  ##Write a "auto-crt.bat"
    child = subprocess.Popen("auto-crt.bat", cwd=getpath, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    stdout, stderr = child.communicate()
    print(child.returncode)

def getbat(batfile): ##Get an auto-crt.bat
    with open(batfile,'w+') as batman:
        batman.write("@echo off")
        batman.write("\n")
        batman.write("start {path}\App\SecureCRT\SecureCRT.exe /SCRIPT {path}\Xunjian-crt.py".format(path=getpath))
        batman.write("\n")
        batman.write(":end")
        batman.write("\n")
        batman.write("exit")
    batman.close()

谢谢你看到最后。

希望对你有帮助。


猜你喜欢

转载自blog.csdn.net/cyx441984694/article/details/80903730