secureCRT脚本—-vbs语法

  • Post author:
  • Post category:其他



常用语句:


1.CRT命令


‘发送命令

crt.Screen.Send “whyy@dsl-vty” & chr(13)

‘等待字符串出现

crt.Screen.WaitForString “Password:”

‘等待字符串出现,等待1秒钟

crt.Screen.WaitForString (“Password:”,1)

‘开启同步

crt.Screen.Synchronous = True

‘判断指针1秒内不移动,用于确定安全连接可以进行后续代码输入操作

Do

bCursorMoved = crt.Screen.WaitForCursor(1)

Loop until bCursorMoved = False

‘判定是否已经连接到设备

if crt.Session.Connected = true then

‘断开连接

crt.session.disConnect

‘激活窗口 用于多tab页面

crt.Activate

‘连接到指定设备

crt.session.Connect(“/telnet ” & b(n))

‘延时1秒

crt.Sleep 1000


2.vbScript语句

a.文件操作

Set fso = CreateObject(“Scripting.FileSystemObject”) ‘创建文件对象

Set file1 = fso.OpenTextFile(“c:\test.txt”,1, False) ‘设置文件对象 1 只读模式;2 写模式;8 在文件末尾进行写操作

objFso.FileExists(“文件路径”) ‘判定文件是否存在,存在返回trus,否则返回false

file1.AtEndOfStream <> True ‘如果文件末尾无数据返回true,否则返回false

line = file1.ReadLine ‘读取第一行文本数据,并将读取位移动到下一行

params = Split (line) ‘分割读取的行数据,用空格符分割

file1.Close ‘关闭文件

b.系统文档操作

‘创建shell对象,对系统特殊文档操作

Set objShell = CreateObject(“WScript.Shell”)

‘返回”我的文档”的路径给变量szMyDocs,可以动态的返回”我的文档”的路径

szMyDocs = objShell.SpecialFolders(“MyDocuments”)

‘设置文件路径到变量中

szSessionsFile = szMyDocs & “\SessionList.txt”


3.知识点


<1.判断文件是否存在

if Not objFso.FileExists(szSessionsFile) then ‘如果txt文件不存在,就报错并退出sub

crt.Dialog.MessageBox “Session list file not found:” & vbcrlf & _

vbcrlf & _

szSessionsFile & vbcrlf & vbcrlf & _

“Create a session list file as described in the description of ” & _

“this script code and then run the script again.”

exit sub

end if

<2.容错机制

On Error Resume Next ‘后面代码错误会继续执行

crt.Session.Connect “/telnet ” & vSessionsArray(0)

szError = Err.Description ‘将错误表述信息放入变量szError中

On Error Goto 0 ‘关闭容错机制

<3.判断是否安全连接

‘判断是否连接正常

if crt.Session.Connected then

crt.Screen.Synchronous = True ‘同步显示

‘第一次连接时的安全检测

Do

bCursorMoved = crt.Screen.WaitForCursor(1)

Loop until bCursorMoved = False

’ 一旦光标在一秒内停止移动,我们就认为他已经安全的连接上了远程设备

end if

<4.安全关闭连接

’ 断开连接

crt.session.disconnect

’ 等待连接关闭

Do

crt.Sleep 100

Loop While crt.Session.Connected = True ‘每100毫秒判断连接是否断开

<5.说明:

1.CRT默认tab的index为1

2.要创建第二个标签页tab,必须使用函数set newtab = objCurrentTab1.Clone()复制标签页为新的tab页面

否则报错tab 2 超出index范围rang

函数:

Set objCurrentTab1 = crt.GetTab(1) ‘创建标签页1

objCurrentTab1.session.disConnect ‘断开标签页1的当前连接

objCurrentTab1.Activate ‘激活标签页1

objCurrentTab1.session.Connect(“/telnet 172.16.205.1” ) ‘标签页1连接设备172.16.205.1

set newtab = objCurrentTab1.Clone() ‘复制标签页1到新标签页


示例:


【示例一】

 $language = "VBScript"
$interface = "1.0"
'本脚本示范:自动登录到某台3550交换机设备,自动输入远程登录密码,同时自动进入特权模式
'自动输入特权密码,同时运行show version命令

Sub Main
  crt.Screen.Synchronous = True
  '通过telnet连接到192.168.1.1
  crt.Session.Connect "/TELNET 192.168.1.1"
  crt.Screen.WaitForString "Password:"
  crt.Screen.Send "cisco" & vbCr
  crt.Screen.waitForString  ">"
  crt.Screen.Send "enable" & vbcr
  crt.Screen.WaitForString "Password:"
  crt.Screen.Send "cisco" & vbcr
  crt.Screen.waitForString "#"
  crt.Screen.Send "show version" & vbcr
  crt.Screen.waitForString "--More--"
  crt.Screen.Send " "
  crt.Screen.Synchronous = False
End Sub

【示例二】

 $language = "VBScript"
 $interface = "1.0"
'本脚本示范:从一个文件里面自动读取设备IP地址,密码等,自动将设备配置备份

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:\config.txt",Forreading, False)        
    crt.Screen.Synchronous = True
    DO While file1.AtEndOfStream <> True
       '读出每行
       line = file1.ReadLine
       '分离每行的参数 IP地址 密码 En密码
       params = Split (line)
       'Telnet到这个设备上
       crt.Session.Connect "/TELNET " & params(0)
       '输入telnet密码
       crt.Screen.WaitForString "Password:"
       crt.Screen.Send params(1) & vbcr
       '进特权模式
       crt.Screen.Send "enable" & vbcr
       crt.Screen.WaitForString "Password:"
       crt.Screen.Send params(2) & vbcr
       crt.Screen.waitForString "#"
        '备份目前Startup配置
       str1 = "copy startup tftp://192.168.1.2/"
       str1 = str1 & "backup(" & params(0) &")" & ".cfg"
       crt.Screen.Send str1 & vbcr
       crt.Screen.WaitForString "]?"
       crt.Screen.Send vbcr
       crt.Screen.WaitForString "]?"
       crt.Screen.Send vbcr
       '备份完成后退出
       crt.Screen.waitForString "#",3
       crt.Session.Disconnect

       loop
    crt.Screen.Synchronous = False           
End Sub

config.txt文本文件为事先编辑好的

格式如:

192.168.1.1 cisco cisco

192.168.1.254 cisco cisco

前面是IP地址,后面是telnet密码,最后是enable密码

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:\config.txt",Forreading, False)        
    crt.Screen.Synchronous = True
    DO While file1.AtEndOfStream <> True
       '读出每行
       line = file1.ReadLine
       '分离每行的参数 IP地址 密码 En密码
       params = Split (line)
       'Telnet到这个设备上
       crt.Session.Connect "/TELNET " & params(0)
       '输入telnet密码
       crt.Screen.WaitForString "Password:"
       crt.Screen.Send params(1) & vbcr
       '进特权模式
       crt.Screen.Send "enable" & vbcr
       crt.Screen.WaitForString "Password:"
       crt.Screen.Send params(2) & vbcr
       crt.Screen.waitForString "#"
        '备份目前Startup配置
       str1 = "copy startup tftp://192.168.1.2/"
       str1 = str1 & "backup(" & params(0) &")" & ".cfg"
       crt.Screen.Send str1 & vbcr
       crt.Screen.WaitForString "]?"
       crt.Screen.Send vbcr
       crt.Screen.WaitForString "]?"
       crt.Screen.Send vbcr
       '备份完成后退出
       crt.Screen.waitForString "#",3
       crt.Session.Disconnect

       loop
    crt.Screen.Synchronous = False           
End Sub

【示例三】

批量添加静态arp:

Sub Main
  Const ForReading = 1
  Dim fso,file,line
  set fso = CreateObject("Scripting.FileSystemObject")
  set file = fso.OpenTextFile("arp.txt",ForReading,False)
  crt.Screen.Synchronous = True
  DO While file.AtEndOfStream <> True
  line = file.ReadLine
  crt.Screen.Send line & vbcr
  crt.Sleep 100
  loop
  crt.Screen.Synchronous = False
End Sub

arp.txt如下:

arp static ip 12.1.1.025 mac 00:24:93:01:73:18 interface ethernet 0/0/1
arp static ip 12.1.1.026 mac 00:24:93:01:73:19 interface ethernet 0/0/1
arp static ip 12.1.1.027 mac 00:24:93:01:73:1a interface ethernet 0/0/1
arp static ip 12.1.1.028 mac 00:24:93:01:73:1b interface ethernet 0/0/1
arp static ip 12.1.1.029 mac 00:24:93:01:73:1c interface ethernet 0/0/1
arp static ip 12.1.1.030 mac 00:24:93:01:73:1d interface ethernet 0/0/1
arp static ip 12.1.1.031 mac 00:24:93:01:73:1e interface ethernet 0/0/1
arp static ip 12.1.1.032 mac 00:24:93:01:73:1f interface ethernet 0/0/1
arp static ip 12.1.1.033 mac 00:24:93:01:73:20 interface ethernet 0/0/1
arp static ip 12.1.1.034 mac 00:24:93:01:73:21 interface ethernet 0/0/1

参考文献:


http://www.jianshu.com/p/2d3b21496f2a



http://www.justwinit.cn/post/2295/



版权声明:本文为sun172270102原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。