SecureCRT and Python

SecureCRT is one of my favourite tools, today I had a chance to use it in a way I didn’t expected.

A customer called with a problem: he has a radio bridge that sometimes loses connection with the remote unit. The workaround is to reload the base unit. The process is manual, he monitors the radio bridge status and reload when required.

Since the radio bridge is not business critical he doesn’t have budget to replace tha radio bridge or troubleshoot the problem, the request was to automate the check/reload process.

I checked the available tools: no Linux servers, no monitoring, no management platform… just a Windows server and… SecureCRT! A quick look on SecureCRT website confirmed that it supports scripting with Python, VBScript, Jscript and PerlScript. I’m familiar with Python so it is my choice.

The idea is simple:

  • connect to the remote device via telnet

  • verify the radio bridge is up

  • reload the device if radio bridge is down

The script with comments is quite self explanatory:

# $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 = "myUsername"
    password = "myPassword"
    enablePassword = "myEnablePassword"
    prompt = "#"

    # PARAMETERS
    command = "sh dot11 associations"
    searchString = "6cfa.8923.beef"
    reloadCommand = "reload in 1 reason REMOTE_NOT_FOUND"

    # 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")
        #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()

This script is scheduled to run every 30 minutes on the Windows server using this sintax:

"c:\Program Files\SecureCRT\securecrt.exe" /script c:\script\checkRadioBridge.py

I’d have prefered a better implementation on a Linux machine but this works so far, the customer is happy and I learnt something new: win-win!

###Useful links

 
comments powered by Disqus