本Blog所有内容不得随意转载,版权属于作者所有。如需转载请与作者联系(
fastzch@163.com
)。
未经许可的转载,本人保留一切法律权益。
一直以来,发现有某些人完全不尊重我的劳动成果,随意转载,提醒一下那些人小心哪天惹上官司。
SecureCRT支持ActiveX script engines,包括VBScript和JScript (Microsoft’s version of JavaScript)两种脚本。
Note:如果你想使用脚本自动运行方式的话,需要在session option中进行设置。 脚本可以用任何文本编辑器来开发。Script头部用来识别脚本语言和SecureCRT脚本接口的版本,脚本头部的每一行必须以#开头,头部包括
$language行和
$interface行。例如,下面是一个简单的脚本。
# $language = "VBScript"
# $interface = "1.0"
Sub Main
' Display SecureCRT's version
MsgBox "SecureCRT version is: " & crt.Version
End Sub 代码通常放在main子过程,引擎在执行main子过程前,将转换并执行全局代码(脚本代码定义在任何子过程之外),如果你有一些初始化处理的话,可以使用这一特性。
如果想终止main过程,用VBScript的话,可以用Exit Sub语句。如:
Sub Main
condition = DoSomething()
If condition = 0 Then
' Error, bailout
Exit Sub
End If
End Sub SecureCRT有一些内建的对象,可以通过顶级应用对象或子对象调用。这些对象都有一些属性和方法。例如:
Dim dlg
Set dlg = crt.Dialog
dlg.Prompt("Login:")
下面分享给大家一个通过N个跳板机上传文件的FTP脚本:
#$language = "VBScript"
#$interface = "1.0"
Sub main
' turn on synchronous mode so we don't miss any data
crt.Screen.Synchronous = True
' Wait for a string that looks like "login: " or "Login: "
' Occasionally, a host needs to be kicked to display a
' login prompt.
If (Not crt.Screen.WaitForString("ogin: ", 5)) Then
crt.Screen.Send vbCr
crt.Screen.WaitForString "ogin: "
End If
crt.Screen.Send "user0" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "pwd0" & VbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "cd /tmp" & VbCr
crt.Screen.WaitForString "#"
'ftp to Machine1
crt.Screen.Send "ftp IP1" & VbCr
crt.Screen.WaitForString "): "
crt.Screen.Send "user1" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "pwd1" & VbCr
crt.Screen.WaitForString "ftp> "
filename = crt.Dialog.Prompt("Please enter your filename.", "Enter filename", "", false)
crt.Screen.Send "cd /tmp" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "binary" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "put " + filename & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "bye" & VbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "telnet IP1" & VbCr
If (Not crt.Screen.WaitForString("ogin: ", 5)) Then
crt.Screen.Send vbCr
crt.Screen.WaitForString "ogin: "
End If
crt.Screen.Send "user1" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "pwd1" & VbCr
crt.Screen.WaitForString ">"
crt.Screen.Send "cd /tmp" & VbCr
crt.Screen.WaitForString ">"
'ftp to Machine2
crt.Screen.Send "ftp IP2" & VbCr
crt.Screen.WaitForString "): "
crt.Screen.Send "user2" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "pwd2" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "cd /tmp" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "binary" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "put " + filename & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "bye" & VbCr
crt.Screen.WaitForString ">"
crt.Screen.Send "telnet IP2" & VbCr
If (Not crt.Screen.WaitForString("ogin: ", 5)) Then
crt.Screen.Send vbCr
crt.Screen.WaitForString "ogin: "
End If
crt.Screen.Send "user2" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "pwd2" & VbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "cd /tmp" & VbCr
crt.Screen.WaitForString "#"
'ftp to Machine3
crt.Screen.Send "ftp IP3" & VbCr
crt.Screen.WaitForString "): "
crt.Screen.Send "user3" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "pwd3" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "cd /tmp" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "binary" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "put " + filename & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "bye" & VbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "telnet IP3" & VbCr
If (Not crt.Screen.WaitForString("ogin: ", 5)) Then
crt.Screen.Send vbCr
crt.Screen.WaitForString "ogin: "
End If
crt.Screen.Send "user3" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "pwd3" & VbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "cd /tmp" & VbCr
crt.Screen.WaitForString "#"
'ftp to Machine4
crt.Screen.Send "ftp IP4" & VbCr
crt.Screen.WaitForString "): "
crt.Screen.Send "user4" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "pwd4" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "binary" & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "put " + filename & VbCr
crt.Screen.WaitForString "ftp> "
crt.Screen.Send "bye" & VbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "telnet IP4" & VbCr
If (Not crt.Screen.WaitForString("ogin: ", 5)) Then
crt.Screen.Send vbCr
crt.Screen.WaitForString "ogin: "
End If
crt.Screen.Send "user4" & VbCr
crt.Screen.WaitForString "assword:"
crt.Screen.Send "pwd4" & VbCr
crt.Screen.WaitForString ">"
crt.Screen.Synchronous = False
End Sub
posted on 2008-09-29 12:51
Robin's Programming World 阅读(9139)
评论(4) 编辑 收藏 所属分类:
其它