一、xshell脚本添加方法

1.1加入会话,每次打开自动运行

1.2 进入——>工具——>脚本,手动运行

二、相关函数和参数

xshell官方技术手册

2.1 xsh.Session

下面的函数或变量在xshell会话中使用,使用的时候要指定会话一起使用,比如使用Sleep()函数,要这样使用:xsh.Session.Sleep(1000)

2.1.1函数
返回值 函数 参数 说明
void Open(LPCTSTR lpszSession) lpszSession:字符串,指Xshell会话路径或Xshell使用的URL类型。 打开新会话或URL。需要把 /s选项置于字符串的前端。例如要打开A.xsh会话使用‘/s $PATH/A.xsh’
void Close() 关闭当前连接的会话。
void Sleep(long timeout) Timeout:毫秒单位的时间值 Xshell按照指定时间进行待机。
void LogFilePath(LPCTSTR lpszNewFilePath) lpszNewFilePath:包括路径在内的文件名 指定日志文件。
void StartLog() 开始会话的日志记录。日志将被保存到LogFilePath()指定的路径。如果没有指定日志文件路径则使用默认路径。
void StopLog() 停止日志记录。
2.1.2 变量
名称 类型 说明
Connected BOOL 检查当前会话是否连接。
LocalAddress BSTR 导入本地地址。
Path BSTR 导入当前会话文件的路径。
RemoteAddress BSTR 导入远程地址。
RemotePort long 导入远程端口号。
Logging BOOL 检查当前会话是否记录日志。
LogFilePath BSTR 保存为日志文件。

2.2 xsh.Screen

下面的函数和变量在处理xshell终端屏幕的时候使用,使用的时候要配合xsh.Screen一起使用,比如要使用Clear()函数,要这样使用:xsh.Screen.Clear

2.2.1函数
返回值 函数 参数 说明
void Clear() 清除终端屏幕。
void Send(LPCTSTR lpszStrToSend) lpszStrToSend:用户拟要发送的字符串 向终端发送消息。
BSTR Get(long nBegRow, long nBegCol, long nEndRow, long nEndCol) nBegRow:终端的行起始位置
nBegCol:终端的列起始位置
nEndRow:终端的行末端位置
nEndCol:终端的列末端位置
读入终端规定区域的字符串并返回读取值。
void WaitForString(LPCTSTR lpszString) lpszString:终端中打印的字符串 等待终端打印lpszString字符串。
Long WaitForStrings(VARIANT FAR* strArray, long nTimeout) strArray:终端中打印的字符串 nTimeout:等候时间 返回值:发现的字符数 等待某消息直到超时。
2.2.2 变量
名称 类型 说明
CurrentColumn long 返回当前列数。
CurrentRow long 返回当前行数。
Columns long 返回与终端的列宽相同的列数。
Rows long 返回与终端的行高相同的行数。
Synchronous BOOL 设置屏幕同步 (True:屏幕同步,false:屏幕不同步)​

2.3 xsh.Dialog

使用的时候要配合xsh.Dialog一起使用,比如要使用MsgBox()函数,要这样使用:xsh.Dialog.MsgBox()

2.3.1 函数
返回值 函数 参数 说明
Long MsgBox(LPCTSTR lpszMsg) LpszMsg:想要发送的字符串 打开一个消息框
string Prompt(LPCTSTR lpszMessage, LPCTSTR lpszTitle, LPCTSTR lpszDefault, BOOL bHidden) lpszMessage:在对话框上显示的字符串。
lpszTitle:在对话框标题栏显示的字符串。
lpszDefault:在对话框输入框中初始显示的字符串。
bHidden:如果设置为True,输入会被隐藏 (e.g. *)
作用:返回用户在对话框中的输入。
返回值:用户在对话框中的输入。
int MessageBox(LPCTSTR lpszMessage, LPCTSTR lpszTitle, int nType) lpszMessage:在消息框中显示的字符串。
lpszTitle:在消息框标题栏显示的字符串。
nType:按钮类型,参考下面的表。
作用:按照用户选择的按钮类型显示消息框并返回相应的值。
返回值:参考下面的表。
2.3.2 变量
类型 Button 返回值
0 OK 1
1 OK / Cancel 1 / 2
2 Abort / Retry / Ignore 3 / 4 / 5
3 Yes / No / Cancel 6 / 7 / 2
4 Yes / No 6 / 7
5 Retry / Cancel 4 / 2
6 Cancel / TryAgain / Continue 2 / 10 / 11

三、参考用例

3.1 官方参考用例

Sub Main

' *** Connect the session ***
xsh.Session.Open "ssh://192.168.1.17"
' "/s C:\Users\Administor\AppData\Roaming\NetSarang\Xshell\Sessions\example.xsh"

xsh.Screen.Synchronous = true

xsh.Screen.WaitForString "login: "
xsh.Screen.Send "username"
xsh.Screen.Send VbCr
xsh.Session.Sleep 100

xsh.Screen.WaitForString "Password: "
xsh.Screen.Send "password"
xsh.Screen.Send VbCr
xsh.Session.Sleep 100

' *** Wait for Prompt Message ***
xsh.Screen.WaitForString "username@password"

' *** Set File Format ***
Dim app, wb, ws
Set app= CreateObject("Excel.Application")
Set wb = app.Workbooks.Add
set ws = wb.Worksheets(1)

xsh.Session.LogFilePath = "c:\example.log"
xsh.Session.StartLog

Dim waitStrs
waitStrs = Array(Chr(10), "username@password") ' make wait message as array

Dim row, screenrow, readline, itmes
row = 1

' *** Send Command ***
xsh.Screen.Send "cat /etc/passwd"
xsh.Screen.Send VbCr
xsh.Session.Sleep 100

Dim result

' *** Read Data and Save it as an EXCEL File ***
Do
While true
result = xsh.Screen.WaitForStrings(waitStrs, 1000)

If result = 2 Then
Exit Do
End If

screenrow = xsh.Screen.CurrentRow - 1
readline = xsh.Screen.Get(screenrow, 1, screenrow, 40)
items= Split(readline, ":", -1)

ws.Cells(row,1).Value = items(0)
ws.Cells(row,2).Value = items(2)

row = row + 1
Wend
Loop

wb.SaveAs("C:\chart.xls") ' save file path
wb.Close
app.Quit

Set ws = nothing
Set wb = nothing
Set app = nothing

xsh.Screen.Synchronous = false

xsh.Session.StopLog

End Sub

3.2 自写的一个自动登录串口并不断重启的一个挂机脚本

说明
1.第一次写VBS的脚本,语法可能会有不规范;
2.这里每一次重启后都是等待的延时,最开始用的字符串判断,但是这个串口会不断地打印很多数据,且时间不固定,虽然每次都会一定出现某一字符串,但是经过测试,xsh.Screen.WaitForString的检测总会出现检测不到的情况,尤其这种检测一大段字符串的情况。所以这里直接使用了延时;
3.下面几个判断,如xsh.Screen.WaitForStrings(“login”, 2000) = 0 ,文档写的此函数的返回值是发现的字符数,按理说应该是 >0或者!=0,这里等于0不就是没有发现吗? 但是此判断条件确实无误,未识别会反复识别且不进入下一判断,而且挂机了1000多次也没问题。 虽然功能完成,但是和文档描述不符,有些疑惑,如有知道者望指导。

Sub main
    Dim i, j, test_count, wait_times, flag1
    i = 0
    j = 0
    test_count = 100
    wait_times = 10
    flag1 = 1

    xsh.Screen.Synchronous = true
    do while i < test_count
        xsh.Session.Sleep 80000
        xsh.Screen.Send VbCr
        xsh.Session.Sleep 300
        flag2 = 0
        do
            flag1 = 0
            if xsh.Screen.WaitForStrings("login", 2000) = 0 Then
                xsh.Screen.Send "root" & VbCr
                xsh.Session.Sleep 300

                if xsh.Screen.WaitForStrings("Password", 2000) = 0 Then
                    xsh.Screen.Send "passsword" & VbCr
                    xsh.Session.Sleep 300

                    do while xsh.Screen.WaitForStrings("root@", 2000) = 0
                        xsh.Screen.Send VbCr
                        if xsh.Screen.WaitForStrings("root@", 1000) <> 0 Then
                            flag2 = 1
                            exit do
                        else
                            flag1 = 1
                        end if
                    loop    

                else
                    xsh.Screen.Send VbCr
                    xsh.Session.Sleep 300
                    flag1 = 0
                end if
            else
                xsh.Screen.Send VbCr
                xsh.Session.Sleep 300
                flag1 = 0
            end if

            if flag2 = 1 Then
                exit do
            end if
        loop while flag1 <> 1


        do while j < wait_times
            xsh.Session.Sleep 1000
            j = j + 1
        loop
        xsh.Screen.Send VbCr
        xsh.Session.Sleep 300
        xsh.Screen.Send "reboot" & VbCr
        i = i + 1
        xsh.Session.Sleep 20000
        j = 0
    loop
End Sub

四、待补充SecureCRT

后面再补充SecureCRT的相关知识,发现SecureCRT支持的函数比xshell要多要完善,但是吧,它的界面有点老,用起来不太舒服,所以还没有深入研究。但是查找资料得知,其支持的函数和资料确实丰富。