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:

 1# $language = "Python"
 2# $interface = "1.0"
 3
 4# Website: www.ifconfig.it
 5# License: https://creativecommons.org/licenses/by-sa/4.0/legalcode
 6
 7def main():
 8
 9  # CONNECTION DATA
10  hostIP = "X.X.X.X"
11  username = "myUsername"
12  password = "myPassword"
13  enablePassword = "myEnablePassword"
14  prompt = "#"
15  
16  # PARAMETERS
17  command = "sh dot11 associations"
18  searchString = "6cfa.8923.beef"
19  reloadCommand = "reload in 1 reason REMOTE_NOT_FOUND"
20  
21  # LOGIN STRINGS
22  loginStrings = ["Username","Password:",">","Password:"]
23  sendString = [username,password,"enable",enablePassword]
24  
25  crt.Screen.Synchronous = True
26  
27  # CONNECT TO REMOTE DEVICE  
28  crt.Session.Connect("/telnet "+hostIP)
29  
30  # AUTHENTICATE PROCESS
31  
32  if (crt.Session.Connected):
33    i=0
34    for string in loginStrings:
35      crt.Screen.WaitForString(string)
36      crt.Screen.Send(sendString[i]+"\r")
37      i = i+1
38      
39      crt.Screen.WaitForString(prompt)
40      crt.Screen.Send("term len 0"+"\r")
41      
42      crt.Screen.WaitForString(prompt)
43      
44      # EXECUTE VERIFICATION COMMAND AND GET OUTPUT
45      crt.Screen.Send(command+"\r")
46      
47      crt.Screen.IgnoreEscape = True
48      
49      result = crt.Screen.ReadString(prompt)
50      result.strip()
51      
52      crt.Screen.IgnoreEscape = False
53      
54      #SEARCH STRING IN OUTPUT
55      
56      if searchString not in result:
57        #crt.Dialog.MessageBox("FOUND STRING")
58        #else:
59        #crt.Dialog.MessageBox("STRING NOT FOUND")
60        crt.Screen.Send("\r")
61        crt.Screen.WaitForString(prompt)
62        crt.Screen.Send(reloadCommand+"\r")
63        
64        # EXIT THE SESSION
65        crt.Screen.Send("\r")
66        crt.Screen.WaitForString(prompt)
67        crt.Screen.Send("exit"+"\r")
68    
69        crt.Screen.Synchronous = False
70
71main()

On the Windows server this script was scheduled to run every 30 minutes using this syntax:

1"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!