一、xshell脚本添加方法
1.1 加入会话,每次打开自动运行
1.2 进入——>工具——>脚本,手动运行
二、相关函数和参数
xshell官方技术手册
2.1 xsh.Session
下面的函数或变量在xshell会话中使用,使用的时候要指定会话一起使用,比如使用Sleep()函数,要这样使用:xsh.Session.Sleep(1000)
2.1.1函数
2.1.2 变量
2.2 xsh.Screen
下面的函数和变量在处理xshell终端屏幕的时候使用,使用的时候要配合xsh.Screen一起使用,比如要使用Clear()函数,要这样使用:xsh.Screen.Clear
2.2.1函数
2.2.2 变量
2.3 xsh.Dialog
使用的时候要配合xsh.Dialog一起使用,比如要使用MsgBox()函数,要这样使用:xsh.Dialog.MsgBox()
2.3.1 函数
2.3.2 变量
三、参考用例
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要多要完善,但是吧,它的界面有点老,用起来不太舒服,所以还没有深入研究。但是查找资料得知,其支持的函数和资料确实丰富。
评论(0)
您还未登录,请登录后发表或查看评论