Experience using Script script in SecureCRT_PHP tutorial

WBOY
Release: 2016-07-12 08:53:25
Original
2273 people have browsed it

Experiences on using Script scripts in SecureCRT

Using VBSCRIPT scripts in SecureCRT can indeed improve our work efficiency and achieve complete automation.

SecureCRT provides us with a very good platform - script tool production and operation. The following is an explanation of several functions commonly used by SecureCRT tools:

1. In SecureCRT, the most commonly used function is crt.Screen. Basically, many operations are based on the return word of the screen to determine the next step. What to do:

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

This function is a single string judgment, KeyString is the keyword to be found, timewaiting It is a timeout threshold, for example: crt.Screen.WaitForString("people:",5) The meaning of this line of code is that if people: is not detected within 5 seconds, the next statement will be executed. If it is changed to: crt. Screen.WaitForString("people:") means that the next line of code will not be executed until people: appears.

WaitForString has a return value, and the return value is True or False. Therefore, conditional judgment can be made based on the return value to determine the next 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)

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

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

means that when the corresponding character is detected within 5 seconds, the corresponding The index number (the index number starts from 1). If neither is checked, 0 is returned. Therefore, the use of this function can be as follows:

Dim SwitchKey

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

Select case SwitchKey

case 1

MsgBox "Cisco Device"

case 2

MsgBox "Huawei Device"

case 3

MsgBox "H3C Device"

case else

MsgBox "Unknown Device"

End Select

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

, InputBox: Prompt the user to enter parameters

temp = inputbox("Prompt the user to enter the name of the parameter", "Name of the dialog box" ): The input parameters need to be assigned to a certain parameter for use.

, output function msgbox

msgbox "Information dialog box output to the user"

eg. Script to find the area of ​​a square

dim r ,s
r=inputbox("Please enter the side length of the square:", "Program to find the area of ​​a square")
s=r*r
msgbox(s)

-- -------------------------------------------------- ---------------------------------------

Sentence structure:

1. For scripts that are executed sequentially, take an example that is widely available on the Internet, and the example of automatically logging into the system is slightly modified as follows.

# $language = "VBScript"
# $interface = "1.0"

Sub Main
'Connect to host 192.168.0.2
crt.session.Connect(" /telnet 192.168.0.2")
'Wait for the username prompt to log in, the waiting time is 10s
crt.screen.WaitForString "login:",10
'Enter the username and press Enter
crt.screen.send "minico" & Chr(13)
'Wait for the password prompt to log in, the waiting time is 10s
crt.screen.WaitForString "Password:",10
'Enter the password, return Chr
crt.screen.send "123456"

crt.screen.send Chr(13)
End Sub

2. Script to select structure

If ... then ...else... structure and case structure, see basic knowledge examples

3. Loop structure

Script example

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

# $language = "VBScript"
# $interface = "1.0"
'======== ================================================== ===================================='
' Program name: AIX.VBS
' Program description: AIX host system configuration/inspection script
' Author: Zheng Jidong
' Completion time: 2008-5-7
'=============== ================================================== ============================'

'============== ================================================== ============================='
' Program global variable area
'======== ================================================== ==================================='
dim ip

'============================================================================================='
'    程序全局常量区
'============================================================================================='
' button parameter options
Const ICON_STOP = 16                 ' display the ERROR/STOP icon.
Const ICON_QUESTION = 32             ' display the '?' icon
Const ICON_WARN = 48                 ' display a '!' 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

'============================================================================================='
'    程序辅助函数区
'============================================================================================='

'登陆函数
Function login
    '定义IP地址,登陆用户名,密码变量
    dim    passwd
    dim username

    Dim result
    Dim flag
    flag =1
    '断开主机连接
    crt.session.Disconnect

    '开启对话框,取得IP地址,登陆用户名称,密码等变量
    ip = crt.Dialog.Prompt("请输入服务器IP地址:", "AIX", "192.1.1.207", false)
    If (Trim(ip) = "")  Or (ip = IDABORT) Then
        result = crt.Dialog.MessageBox("您没有输入登陆的IP地址,CRT将被退出!", "提示信息",ICON_INFO)
        crt.quit
    End If

    flag =1
    While flag = 1
        username = crt.Dialog.Prompt("请输入登陆用户名:", "AIX", "root", false)
        If     username = IDABORT Then
            result = crt.Dialog.MessageBox("您选择了没有输入用户名称,CRT将被推出!", "提示信息",ICON_INFO)           
            crt.quit
        End If

If (Trim(username) = "")Then
result = crt.Dialog.MessageBox("Please enter the login user name!", "Prompt message", ICON_INFO)
Else
flag = 0
End If
wend

passwd = crt.Dialog.Prompt("Please enter the login user password:", "AIX", "congine", true)

' Connect to the host
crt.screen.Synchronous = true
crt.session.Connect("/telnet " & ip)
'Wait for the username prompt to log in, the waiting time is 10s
crt.screen .WaitForString "login:"
'Enter the username and press Enter
crt.screen.send username & chr(13)

'Wait for the login password prompt to log in, the waiting time is 10s
crt.screen.WaitForString "Password:"
'Enter 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("Server login failed, please check whether the IP address, user name, and password are entered correctly!", "Prompt message", 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("Please enter the location of the LOG file for this session", "AIX", "c:" & ip &".log", false)
If Trim(logfilename) = "" Or (logfilename = IDABORT) then
result = crt.Dialog.MessageBox("It is strongly recommended to save the session log", "Prompt message", 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(cmdstr, sec)
setline
sec = sec * 1000
crt.screen.send cmdstr & Chr(13)
crt.Sleep sec
End Function

'Get basic server information
Function get_machinfo

'Host basic 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 network card status
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

'================================================ ================================================'
' Program main function (main) area
'====================================== ================================================== ======='

'Main function
Sub Main
Dim result
' crt.screen.Synchronous = true
'System login
login

'Write log
writelog

'Get server information
get_machinfo
result = crt.Dialog.MessageBox("Information collection is completed, do you want to launch CRT?", "Prompt 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1124519.htmlTechArticleExperiences on using Script scripts in SecureCRT Using VBSCRIPT scripts in SecureCRT can indeed improve our work efficiency and can be achieved Complete automation. SecureCRT provides us with...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template