Experience in using scripts in SecureCRT

Source: Internet
Author: User

Experience in using scripts in SecureCRT

Using VBSCRIPT in SecureCRT can improve our work efficiency and achieve full automation.

SecureCRT provides a good platform for us to create and run the script tool. The following describes several common functions of SecureCRT:

1. In SecureCRT, crt. Screen is the most used. Basically, many operations are based on the returned words of the Screen to determine what to do next:

(1): crt. Screen. WaitForString ("KeyString", timewaiting)

This function is a single string judgment, KeyString is the keyword to be searched, timewaiting is a timeout threshold value, such as: crt. screen. waitForString ("people:", 5) the code in this row indicates that no people: is detected within 5 seconds, and the next statement is executed. If it is changed to: crt. screen. waitForString ("people:") means that the next line of code is executed only when people: appears.

WaitForString returns True or False. Therefore, you can determine the condition based on the returned value to determine the code. For example:

If (crt. Screen. WaitForString ("current state: UP", 1) <> False) Then
PortStatus = "PortUP"
Else
PortStatus = "PortDown"
End If

Msgbox portStatus

This code is used to determine the port status and record it.

(2): crt. Screen. WaitForStrings ("KeyString1", "KeyString2",..., timeout)

It is used to judge multiple strings. The timeout function is the same. For example:

Crt. Screen. WaitForStrings ("cisco", "huawei", "H3C", 5)

This means that when a corresponding character is detected within five seconds, the corresponding index number is returned (the index number starts from 1 ). If none of them are found, 0 is returned. Therefore, the function can be used as follows:

Dim SwitchKey

SwitchKey = crt. Screen. WaitForStrings ("cisco", "huawei", "H3C", 5)

Select case SwitchKey

Case 1

MsgBox "Cisco devices"

Case 2

MsgBox "Huawei devices"

Case 3

MsgBox "H3C device"

Case else

MsgBox "unknown device"

End Select

(3) In fact, the script language supported by SecureCRT is VBS, which is significantly different from that supported by VB and has poor interface support. However, there are also several conversational functions.

<1>, InputBox: prompt the user to enter parameters

Temp = inputbox ("prompt you to enter the parameter name", "Name of the dialog box"): You need to assign the input parameter to a parameter for use.

<2> output function msgbox

Msgbox "Information dialog box for user output"

Eg. scripts for Square area

Dim r, s
R = inputbox ("Enter the side length of the Square:", "program for Square area ")
S = r * r
Msgbox (s)

Bytes -------------------------------------------------------------------------------------------

Statement structure:

1. Execute the script in sequence. Here is an example of online flooding. The example of automatic logon to the system is slightly modified as follows.

# $ Language = "VBScript"
# $ Interface = "1.0"

Sub Main
'Connection host 192.168.0.2
Crt. session. Connect ("/telnet 192.168.0.2 ")
'Wait for the login username to prompt login. The wait time is 10 s.
Crt. screen. WaitForString "login:", 10
'Enter the user name and press Enter.
Crt. screen. send "minico" & Chr (13)
'Wait for the logon password to prompt login. The wait time is 10 s.
Crt. screen. WaitForString "Password:", 10
'Enter the password and press Enter.
Crt. screen. send "123456"

Crt. screen. send Chr (13)
End Sub

2. Select the structure script

If... then... else... the structure and case structure can be found in the basic knowledge example.

3. Loop Structure

 

 

 

Script instance

#===================================================== ====================

# $ Language = "VBScript"
# $ Interface = "1.0"
'================================================ ========================================================== ================'
'Program name: AIX. VBS
'Program Description: AIX host system configuration/inspection script
'Author: Zheng Jidong
'Completion time:
'================================================ ========================================================== ================'

'================================================ ========================================================== ================'
'Program global variable area
'================================================ ========================================================== ================'
Dim ip

'================================================ ========================================================== ================'
'Global Constant Area of the program
'================================================ ========================================================== ================'
'Button parameter options
Const ICON_STOP = 16 'display the ERROR/STOP icon.
Const ICON_QUESTION = 32 'display '? 'Icon
Const ICON_WARN = 48 'display '! 'Icon.
Const ICON_INFO = 64 'displays "info" icon.
Const BUTTON_ OK = 0 'OK button only
Const BUTTON_CANCEL = 1 'OK and Cancel buttons
Const BUTTON_ABORTRETRYIGNORE = 2 'abort, Retry, and Ignore buttons
Const BUTTON_YESNOCANCEL = 3 'yes, No, and Cancel buttons
Const BUTTON_YESNO = 4 'Yes and No buttons
Const BUTTON_RETRYCANCEL = 5' Retry and Cancel buttons
Const DEFBUTTON1 = 0 'first button is default
Const DEFBUTTON2 = 256 'second button is default
Const DEFBUTTON3 = 512 'third button is default

'Possible MessageBox () return values
Const IDOK = 1' OK button clicked
Const IDCANCEL = 2' Cancel button clicked
Const IDABORT = 3 'abort button clicked
Const IDRETRY = 4' Retry button clicked
Const IDIGNORE = 5' Ignore button clicked
Const IDYES = 6 'Yes button clicked
Const IDNO = 7' No button clicked

'================================================ ========================================================== ================'
'Program Auxiliary Function Area
'================================================ ========================================================== ================'

'Login Function
Function login
'Define IP address, login user name, password variable
Dim passwd
Dim username

Dim result
Dim flag
Flag = 1
'Disconnect the host
Crt. session. Disconnect

'Open dialog box, get the IP address, login user name, password, and other variables
Ip = crt. Dialog. Prompt ("Enter the Server ip Address:", "AIX", "192.1.1.207", false)
If (Trim (ip) = "") Or (ip = IDABORT) Then
Result = crt. Dialog. MessageBox ("You have not entered the logon IP address, and the CRT will be exited! "," Prompt information ", ICON_INFO)
Crt. quit
End If

Flag = 1
While flag = 1
Username = crt. Dialog. Prompt ("Enter the login username:", "AIX", "root", false)
If username = IDABORT Then
Result = crt. Dialog. MessageBox ("if you have selected the user name, the CRT will be launched! "," Prompt information ", ICON_INFO)
Crt. quit
End If

If (Trim (username) = "") Then
Result = crt. Dialog. MessageBox ("Enter the login user name! "," Prompt information ", ICON_INFO)
Else
Flag = 0
End If
Wend

Passwd = crt. Dialog. Prompt ("Enter your logon password:", "AIX", "congine", true)

'Connect to the host
Crt. screen. Synchronous = true
Crt. session. Connect ("/telnet" & ip)
'Wait for the login username to prompt login. The wait time is 10 s.
Crt. screen. WaitForString "login :"
'Enter the user name and press Enter.
Crt. screen. send username & chr (13)

'Wait for the logon password to prompt login. The wait time is 10 s.
Crt. screen. WaitForString "Password :"
'Enter the password and press Enter.
Crt. screen. send passwd & chr (13)
If crt. screen. WaitForString ("invalid login name or password", 3) = True Then
Result = crt. Dialog. MessageBox ("failed to log on to the server. Check whether the IP address, user name, and password are entered correctly! "," Prompt information ", ICON_INFO)
Crt. quit
End If
Crt. screen. Synchronous = false
End Function

'Record the current session log function
Function writelog
Dim result
Dim logfilename
Dim flag
Flag = 1

While flag = 1
Logfilename = crt. Dialog. Prompt ("Enter the location of the session LOG File", "AIX", "c: \" & ip & ". log", false)
If Trim (logfilename) = "" Or (logfilename = IDABORT) then
Result = crt. Dialog. MessageBox ("we strongly recommend that you save session logs", "prompt information", ICON_INFO)
Else
Flag = 0
End if
Wend
Crt. session. LogFileName = logfilename
Crt. session. Log (true)
End Function

Function setline
Crt. screen. send chr (13) & chr (13)
'Crt. Sleep 1000
End Function

Function setcommand (reverse STR, sec)
Setline
Sec = sec * 1000
Crt. screen. send Response STR & Chr (13)
Crt. Sleep sec
End Function

'Get basic Server Information
Function get_machinfo

'Basic host information
Setcommand "hostname", 1
Setcommand "prtconf | grep 'machine Serial number'", 6
'Host device status
Setcommand "lsdev-C | grep proc", 2
Setcommand "lsattr-El mem0", 2
Setcommand "lsdev-Cc disk", 2
Setcommand "lsdev-Cc adapter", 2
Setcommand "lsdev-Cc tape", 2

'Host Nic Condition
Setcommand "ifconfig-a", 2
Setcommand "more/etc/hosts", 2

'Host Software Information
Setcommand "uname-a", 2
Setcommand "oslevel-s", 5
Setcommand "instfix-I | grep ML", 10

'Host volume group information
Setcommand "lsvg", 2
Setcommand "lsvg-o", 2
Setcommand "lsvg-l rootvg", 2

'Host File System Information
Setcommand "df-g", 2

'Host log information
Setcommand "errpt", 2
Setcommand "errpt-a", 2
Setcommand "sysdumpdev-l", 2

'Host system performance
Setcommand "lsps-a", 2
Setcommand "vmstat 2 10", 25
Setcommand "iostat 2 10", 25

End Function

'================================================ ========================================================== ================'
'Main Function Area
'================================================ ========================================================== ================'

'Main Function
Sub Main
Dim result
'Crt. screen. Synchronous = true
'System Login
Login

'Write logs
Writelog

'Get Server Information
Get_machinfo
Result = crt. Dialog. MessageBox ("after the information is collected, is the CRT available? "," Message ", ICON_QUESTION Or BUTTON_YESNO Or DEFBUTTON2)
If result = IDYES Then
Crt. quit
End If

'End session log
Crt. session. Log (false)
'Crt. screen. Synchronous = false
End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.