SecureCRT and Python
SecureCRT is one of my favorite tools. Today I had a chance to use it in a way I didn’t expected.
The problem
A customer called about a problem with a radio link that sometimes loses connection with the remote unit.
The workaround was to reload the base unit to restore the link. The process was manual: the customer monitors the radio bridge status and do a reload when required.
Since the radio bridge is not considered business critical didn’t have budget to replace the faulty equipment or troubleshoot the issue. The request was to automate the check/reload process with minimal effort.
The solution
I did a wide analysis of the available tools on the site: no Linux servers, no monitoring software, no NMS platform… just a Windows server and… SecureCRT !
With a quick look at the SecureCRT documentation I noticed that it supports scripting with Python , VBScript, Jscript and PerlScript. I’m familiar with Python so it was my frist choice.
The basic idea for the script was this:
- connect to the remote device via telnet
- verify if the radio bridge is up
- if the radio bridge is down reload the device
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()
On the Windows server this script was scheduled to run every 30 minutes using this syntax:
"c:\Program Files\SecureCRT\securecrt.exe" /script c:\script\checkRadioBridge.py
I’d have preferred a better implementation on a Linux machine but this works so far, the customer is happy and I learned something new: that’s a win-win !