Python Login Script for Cisco 2k Series Routers and display the command output in the window - Part2

Another Way to login to SecureCrt using python if there is ">" operator is as follows :

This below script will help you login as well as search for the string mentioned at the beginning and out if found or not

**************************
# $language = "Python"
# $interface = "1.0"

# Website: www.ifconfig.it
# License: https://creativecommons.org/licenses/by-sa/4.0/legalcode

def main():

    # CONNECTION DATA
    hostIP = "X.X.X.X"
    username = "XXXXX"
    password = "XXXX"
    enablePassword = "XXXXX"
    prompt = "#"

    # PARAMETERS
    command = "XXXXX"
    searchString = "XXXXX"
    reloadCommand = "XXXX"

    # LOGIN STRINGS
    loginStrings = ["Username","Password:",">","Password:"]
    sendString = [username,password,"enable",enablePassword]

    crt.Screen.Synchronous = True

    # CONNECT TO REMOTE DEVICE 
    # crt.Session.Connect("/telnet "+hostIP)

    # AUTHENTICATE PROCESS

    if (crt.Session.Connected):

        i=0
        for string in loginStrings:
            crt.Screen.WaitForString(string)
            crt.Screen.Send(sendString[i]+"\r")
            i = i+1

        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("term len 0"+"\r")

        crt.Screen.WaitForString(prompt)

        # EXECUTE VERIFICATION COMMAND AND GET OUTPUT
        crt.Screen.Send(command+"\r")

        crt.Screen.IgnoreEscape = True

        result = crt.Screen.ReadString(prompt)
        result.strip(" ")

        crt.Screen.IgnoreEscape = False

        #SEARCH STRING IN OUTPUT

        if searchString not in result:
            crt.Dialog.MessageBox("FOUND STRING"+"\n"+ result)
        else:
            crt.Dialog.MessageBox("STRING NOT FOUND")
            crt.Screen.Send("\r")
            crt.Screen.WaitForString(prompt)
            #crt.Screen.Send(reloadCommand+"\r")

        # EXIT THE SESSION
        crt.Screen.Send("\r")
        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("exit"+"\r")   

        crt.Screen.Synchronous = False

main()

**************************

Credit : https://www.ifconfig.it/hugo/post/2015-06-01-scrtpython/

Comments

Popular posts from this blog

Python Login Script for Cisco 2k Series Routers and display the command output in the window