使用VBS编写xshell/SecureCRT自动化脚本

一、xshell脚本添加方法

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

在这里插入图片描述

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

在这里插入图片描述

二、相关函数和参数

xshell官方技术手册

2.1 xsh.Session

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

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

2.2 xsh.Screen

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

2.2.1函数
返回值函数参数说明
voidClear()清除终端屏幕。
voidSend(LPCTSTR lpszStrToSend)lpszStrToSend:用户拟要发送的字符串向终端发送消息。
BSTRGet(long nBegRow, long nBegCol, long nEndRow, long nEndCol)nBegRow:终端的行起始位置
nBegCol:终端的列起始位置
nEndRow:终端的行末端位置
nEndCol:终端的列末端位置
读入终端规定区域的字符串并返回读取值。
voidWaitForString(LPCTSTR lpszString)lpszString:终端中打印的字符串等待终端打印lpszString字符串。
LongWaitForStrings(VARIANT FAR* strArray, long nTimeout)strArray:终端中打印的字符串 nTimeout:等候时间 返回值:发现的字符数等待某消息直到超时。
2.2.2 变量
名称类型说明
CurrentColumnlong返回当前列数。
CurrentRowlong返回当前行数。
Columnslong返回与终端的列宽相同的列数。
Rowslong返回与终端的行高相同的行数。
SynchronousBOOL设置屏幕同步 (True:屏幕同步,false:屏幕不同步)​

2.3 xsh.Dialog

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

2.3.1 函数
返回值函数参数说明
LongMsgBox(LPCTSTR lpszMsg)LpszMsg:想要发送的字符串打开一个消息框
stringPrompt(LPCTSTR lpszMessage, LPCTSTR lpszTitle, LPCTSTR lpszDefault, BOOL bHidden)lpszMessage:在对话框上显示的字符串。
lpszTitle:在对话框标题栏显示的字符串。
lpszDefault:在对话框输入框中初始显示的字符串。
bHidden:如果设置为True,输入会被隐藏 (e.g. *****)
作用:返回用户在对话框中的输入。
返回值:用户在对话框中的输入。
intMessageBox(LPCTSTR lpszMessage, LPCTSTR lpszTitle, int nType)lpszMessage:在消息框中显示的字符串。
lpszTitle:在消息框标题栏显示的字符串。
nType:按钮类型,参考下面的表。
作用:按照用户选择的按钮类型显示消息框并返回相应的值。
返回值:参考下面的表。
2.3.2 变量
类型Button返回值
0OK1
1OK / Cancel1 / 2
2Abort / Retry / Ignore3 / 4 / 5
3Yes / No / Cancel6 / 7 / 2
4Yes / No6 / 7
5Retry / Cancel4 / 2
6Cancel / TryAgain / Continue2 / 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要多要完善,但是吧,它的界面有点老,用起来不太舒服,所以还没有深入研究。但是查找资料得知,其支持的函数和资料确实丰富。

  • 1
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓜洲大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值