SecureCRT脚本之WaitForString函数

在SecureCRT里,crt.Screen应该是用得比较多,很多操作都是基于屏幕的返回内容,来进一步决定接下来的操作。

Screen屏幕返回内容判断

在判断屏幕返回内容的函数里,主要用到的是两个:

  • crt.Screen.WaitForString("KeyString",timeout)
  • crt.Screen.WaitForStrings("KeyString1","KeyString2",...,timeout)

1、WaitForString(单字符) 分析:

第一个函数是单字符串判断,KeyString是需要查找的关键字,timeout是一个超时阀值。

例如:

crt.Screen.WaitForString("Username:",5)

代码的意思是,如果在5秒内没有检测到“Username:”的出现,就会执行下一条语句,

不是设置超时阀值:

crt.Screen.WaitForString("Username:")

这样就会一直等到Username:的出现,才会执行下一行代码。

WaitForString函数是有返回值的(True 或者 False)。因此,可以根据返回值进行条件判断以确定一下条代码。例如:

If (crt.Screen.WaitForString ("port state : UP",1) <> False) Then
   status="UP"
Else
   status="Down"
End If

 这段代码可以用来判断端口状态情况并记录下来。

2、WaitForString(多字符​​​​​​​)

第二个函数用于多个字符串的判断,timeout的作用是一样的。例如:

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

意思是如果在5秒内有检测到相应的字符(cisco,huawei,h3c)时,返回相应的索引号(索引号是从1开始的)。如果都没有检查到,则返回0。因此,该函数的使用可以如下:

Dim SwitchType

SwitchType=crt.Screen.WaitForStrings("cisco","huawei","h3c",5)

Select case SwitchType

    case 1

        MsgBox "思科设备"

    case 2

        MsgBox "华为设备"

    case 3

        MsgBox "华三设备"

    case else

        MsgBox "未知设备"

End Select

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
# $language = "VBScript" # $interface = "1.0" '作者;小怪兽怪瘦 Sub Main Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso,file1,line,str1,params Set fso = CreateObject("Scripting.FileSystemObject") Set file1 = fso.OpenTextFile("D:\backup\list.txt",Forreading, False) crt.window.show 3 crt.Screen.Synchronous = True do while file1.AtEndOfStream True '读取每一行 line = file1.ReadLine '分割IP,用户名,密码 特权密码 型号简写(z m h b) 位置 params = Split (line) ip = params(0) user = params(1) password = params(2) su = params(3) mode = params(4) whereis = params(5) crt.session.LogFileName = "D:\backup\log\"&ip&"_"&whereis&"_20190620.txt" '提供日志,修改后面日期即可 crt.session.Log(true) crt.Session.Connect ( "/SSH2 /L "&user&" /PASSWORD "&password&" "&ip) '建立SSH连接 if mode = "m" then 'm 代表迈普型号 crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "more off" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-mp.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "more on" & chr(13) crt.Screen.WaitForString "#" crt.Screen.Send "exit" & chr(13) crt.Screen.WaitForString ">" crt.Session.Disconnect end if if mode = "c" then 'c 代表思科型号 crt.Screen.WaitForString ">" crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 4" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "length 0" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-cisco.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 4" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "no length" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" crt.Session.Disconnect end if if mode = "r" then 'r 代表锐捷型号 crt.Screen.WaitForString ">" crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '关闭more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 15" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "length 0" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-rj.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) crt.Screen.waitForString "#" loop crt.Screen.Send "show running-config" & chr(13) crt.Screen.WaitForString "#" '打开more提示 crt.Screen.Send "configure terminal" & chr(13) crt.Screen.WaitForString "config)#" crt.Screen.Send "line vty 0 15" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "no length" & chr(13) crt.Screen.WaitForString "line)#" crt.Screen.Send "end" & chr(13) crt.Screen.WaitForString "#" crt.Session.Disconnect end if if mode = "z" then 'z 代表中兴型号 crt.Screen.Send "enable" & chr(13) crt.Screen.WaitForString "assword:" crt.Screen.Send params(3) & chr(13) crt.Screen.WaitForString "#" '执行命令集 Set cmdfile = fso.OpenTextFile("D:\backup\cmd-zx.txt",Forreading, False) do while cmdfile.AtEndOfStream True line2 = cmdfile.ReadLine crt.Screen.Send line2 & Chr(13) if (crt.Screen.WaitForString (" --More--",1)False) then crt.Screen.Send " " & chr(13) else crt.Screen.Send chr(13) end if crt.Screen.waitForString "#" loop 'crt.Screen.Send "show running-config" & chr(13) 'crt.Screen.WaitForString " --More--" 'crt.Screen.WaitForString "#" crt.Session.Disconnect
SecureCRT是一款功能强大的终端仿真软件,它支持脚本编写,并可以通过脚本实现自动化任务。SecureCRT脚本编写主要是通过VBScript来完成的。 在SecureCRT中,通过脚本可以实现诸如连接远程主机、自动登录、执行命令等功能。编写SecureCRT脚本的步骤如下: 1. 打开SecureCRT软件,并点击“脚本”菜单下的“编辑器”选项,在弹出的编辑器中编写脚本。 2. 在脚本中,可以使用诸如`crt.Session.Connect`方法来连接远程主机,使用`crt.Screen.Send`方法发送命令,使用`crt.Screen.WaitForString`方法等待特定字符串的出现。 3. 脚本中还可以通过`crt.Dialog.MessageBox`方法弹出对话框,以便在执行过程中进行交互。 4. 当脚本编写完成后,可以保存为.vbs格式的文件,然后通过SecureCRT脚本运行工具运行脚本。 例如,以下是一个简单的SecureCRT脚本示例,用于连接远程主机并执行命令: ```vbscript ' SecureCRT脚本示例:连接远程主机并执行命令 ' 连接远程主机 crt.Session.Connect "/ssh2 /L username /PASSWORD password hostname" ' 等待登录成功后显示提示符 crt.Screen.WaitForString "prompt> " ' 发送命令 crt.Screen.Send "command1" & vbcr ' 等待命令执行完成 crt.Screen.WaitForString "result1" ' 发送下一个命令 crt.Screen.Send "command2" & vbcr ' 等待命令执行完成 crt.Screen.WaitForString "result2" ' 断开与远程主机的连接 crt.Session.Disconnect ``` 以上是一个简单的SecureCRT脚本示例,用于连接远程主机并执行两个命令。通过编写类似的脚本,可以实现更复杂的自动化任务,提高工作效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值