The main content of this article is transferred from: [SecureCRT python scripting study guide] (https://www.cnblogs.com/zhaoyujiao/p/4908627.html)
SecureCRT supports multiple scripting languages such as VB, JavaScript, and Python.
# $language ="Python"
# $interface="1.0"
# crt.Dialog.FileOpenDialog([title,[buttonLabel,[defaultFilename,[filter]]]])
# A dialog box pops up to select a single file;If a specific file is selected, the absolute path of the file is returned, and if the pop-up window "Cancel" is selected, it returns empty.
filePath = crt.Dialog.FileOpenDialog("please open a file","open","a.log","(*.log)|*.log")
# filePath = crt.Dialog.FileOpenDialog("","","a.log","")
# crt.Dialog.MessageBox(message,[title,[icon|buttons]])Warning, button type pops up a message box, you can define buttons, use buttons and text messages to realize a simple dialogue with the user;
crt.Dialog.MessageBox(filePath,"",64|0)
crt.Dialog.MessageBox("Session disconnected","session",64|2)
crt.Dialog.MessageBox("Confirm whether to exit","session",32|1)
crt.Dialog.MessageBox("Confirm whether to exit","session",32|3)
crt.Dialog.MessageBox("Whether to continue installation","session",32|4)
crt.Dialog.MessageBox("This conversation is already open","session",48|5)
crt.Dialog.MessageBox("Unable to connect to this window","session",16|6)
# crt.Dialog.Prompt(message [, title [,default[,isPassword ]]])
# An input box pops up, users can fill in text, such as fill in the file name, fill in the path, fill in the IP address, etc.,If you click'ok', Return the input string, otherwise return""
password = crt.Dialog.Prompt("password","session","admin",False)
crt.Dialog.MessageBox(password,"password",64|0)
password = crt.Dialog.Prompt("password","session","",True)
crt.Dialog.MessageBox(password,"password",64|0)
# $language ="Python"
# $interface="1.0"
# CurrentColumn returns the column coordinates of the current cursor.
curCol = crt.Screen.CurrentColumn
crt.Dialog.MessageBox(str(curCol))
# CurrentRow returns the row coordinates of the current cursor.
curRow = crt.Screen.CurrentRow
crt.Dialog.MessageBox(str(curRow))
# Columns returns the maximum column width of the current screen
cols = crt.Screen.Columns
crt.Dialog.MessageBox(str(cols))
# Rows returns the maximum row width of the current screen
rows = crt.Screen.Rows
crt.Dialog.MessageBox(str(rows))
# IgnoreEscape defines whether to obtain Escape characters (special characters such as carriage return) when using the three methods WaitForString, WaitForStrings, and ReadString, which will be obtained by default
crt.Screen.IgnoreEscape = False
crt.Dialog.MessageBox(crt.Screen.ReadString(["\03"],5)) #Get ctrl+c
crt.Screen.IgnoreEscape = True
crt.Dialog.MessageBox(crt.Screen.ReadString(["\03"],2)) #Don't get ctrl+c
# MatchIndex defines that when the three methods of WaitForStrings and ReadString are used, the return value will be obtained according to the position of the parameter, starting from 1 and returning 0 if there is no match.
outPut = crt.Screen.ReadString(["error","warning","#"],10)
index = crt.Screen.MatchIndex
if(index ==0):
crt.Dialog.MessageBox("Timed out!")elif(index ==1):
crt.Dialog.MessageBox("Found 'error'")elif(index ==2):
crt.Dialog.MessageBox("Found 'warning'")elif(index ==3):
crt.Dialog.MessageBox("Found '#'")
# Synchronous Set the synchronization properties of the screen. If set to false, some data may be lost when the WaitForString, WaitForStrings, ReadString functions are used in the script. After setting to true, the screen may freeze. The default is false
crt.Screen.Synchronous = True
crt.Screen.Send("\r\n")
crt.Screen.ReadString("#")
crt.Screen.Send("\r\n")
crt.Screen.WaitForString("#")
crt.Screen.Send("\r\n")
crt.Screen.WaitForStrings(["#",">"])
crt.Screen.Send("conf t\r\n")
# method
# Clear()Clear screen function
# crt.Screen.Clear()
# get()Take out the characters on the screen in a rectangular box according to the coordinates(That is, from one row and one column to other rows and other columns), Does not include the carriage return and line feed in the string, so this is mostly used to get the unformatted string at the cursor or a small segment of the string in a specific area.
out = crt.Screen.Get(row1, col1, row2, col2)
crt.Dialog.MessageBox(out)
# get2()Explain to extract the characters on the screen in a rectangular box according to the coordinates(That is, from one row and one column to other rows and other columns), Contains the carriage return and line feed in the string, so this is mostly used to obtain a large formatted string.
crt.Screen.Get2(row1, col1, row2, col2)
# IgnoreCase uses global parameter settings to control whether the three functions WaitForString, WaitForStrings, and ReadString are case sensitive. The default is false. All uppercase and lowercase strings are checked. When set to true, case sensitivity is not checked.
crt.Screen.IgnoreCase = True
crt.Screen.Send("show memory\r\n")
crt.Screen.WaitForString("more")
crt.Screen.Send("\r\n")
crt.Screen.WaitForStrings("more","#")
crt.Screen.Send("\r\n")
crt.Screen.ReadString("more","#")
# Send()Send a string to the remote device or screen. When sending a string to the screen, you need to specify the second parameter as True
crt.Screen.Send("show version\r\n")
crt.Screen.Send("\r\nhello,world!\r\n",True)
crt.Screen.IgnoreCase = True
while(crt.Screen.WaitForString("more",10)):
crt.Screen.Send("\r\n")
# SendKeys()Send keys to the current window, including combination keys, for example, you can send similar"CTRL+ALT+C"Wait for such a key combination, just write like this: crt.screen.sendkeys("^%c");This function needs language support, currently only VBS and JS scripts can be used.
# SendSpecial()You can send a special control code. This control code is a built-in function of Crt, which can include all the functions provided in the function list of Menu, Telnet, and VT functions.
crt.Screen.SendSpecial("vT_HOLD_SCREEN")
# WaitForCursor()Waiting for the cursor to move, the return value is true when moving, and false when there is a timeout parameter and timeout, otherwise it will always wait for the cursor to move. Use this function to determine whether the output of a command is over,
crt.Screen.WaitForCursor(5)
crt.Screen.Send("\r\nhello,world!\r\n",True)if( crt.Screen.WaitForCursor(5)):
crt.Screen.Send("show version\r\n")
# WaitForKey()It returns true when it detects a keyboard key, and it returns false when there is a timeout parameter and timeout, otherwise it will wait for the key.
if(crt.Screen.WaitForKey(5)):
crt.Screen.Send("show version\r\n")
# WaitForString()Generally used to wait for a string after sending a command
# crt.Screen.WaitForString(string,[timeout],[bCaseInsensitive])
crt.Screen.WaitForString("#",10)
# WaitForStrings()Same function as WaitForString, can wait for multiple strings
outPut = crt.Screen.WaitForStrings(["error","warning","#"],10)
index = crt.Screen.MatchIndex
if(index ==0):
crt.Dialog.MessageBox("Timed out!")elif(index ==1):
crt.Dialog.MessageBox("Found 'error'")elif(index ==2):
crt.Dialog.MessageBox("Found 'warning'")elif(index ==3):
crt.Dialog.MessageBox("Found '#'")
# ReadString()Similar to the WaitForStrings function, it waits for a few characters to appear, the difference is that it also reads all the characters that appear before the string.
crt.Screen.ReadString([string1,string2..],[timeout],[bCaseInsensitive])1. string, a required parameter, the waiting string, at least one, can be a special character such as:\r\n;
2、 timeout, an optional parameter, the timeout period, it will return false when the corresponding string is not detected, and it will wait forever without this parameter;
3、 bCaseInsensitive, optional parameter, case-insensitive, the default value is false, which means that the case of the string will be detected, when it is true, the case will not be detected.
# $language ="python"
# $interface="1.0"
# This automatically generated script may need to be
# edited in order to work correctly.import time
start_time = time.time()
end_time = time.time()
def Main():while True:
end_time = time.time()
crt.Screen.Synchronous = False
crt.Screen.Send("?")
crt.Screen.Send("\r\n")
crt.Screen.Send("\r\n")
time.sleep(1)
running_time = end_time - start_time
msg ="Session disconnected\n"+ \
" running ms : "+str(running_time)+"\n"+ \
" start ms : "+str(start_time)+"\n"+ \
" ent ms : "+str(end_time)+"\n"
crt.Dialog.MessageBox(msg,"session",64|2)Main()
Author: Frytea
Title: Python scripting under SecureCRT
Link: https://blog.frytea.com/archives/469/
Copyright: This work by TL-Song is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Recommended Posts