secure CRT 是一套很有名的終端機連線軟體,不論是 ssh、telnet、serial 都可以很順利的連接。它是要付費的,還不便宜。
主要的客群,是讓工程師在開發 / 測試 / 操作的時候使用。而不是拿來上PTT的。(雖然也是可以上啦)
其中 secure CRT 最常被人使用的功能,應該就是它的可自定義腳本。早期 4.x 版的時候只有支援 vbscript 這種語法。到了目前新的 6.x 或是 7.x 版之後,也開始支援 python 語法的腳本了。
之前在工作上會有需要「一口氣執行多個 script」的需求。在官方的論壇、以及google上雖然有找到一些作法,但終究不太符合我自己的需求。
所以就用官方範例當中的 Include 這個 function 做為基底,修改了一下,達成可以一口氣跑多個 script 的機制。
程式碼在此:
https://gist.github.com/tacolin/4317d6b78751699d36cc
有兩個腳本,script1.vbs 及 script2.vbs。兩個 script 都可以單獨執行。
script1.vbs
#$language = "VBScript" | |
#$interface = "1.0" | |
'Scirpt1.vbs can be excuted by itself' | |
param = 100 | |
Function common_func () | |
msgbox "[script1][common_func] this is common func" | |
End Function | |
Sub Main () | |
' notice: reset the run_return value at the beginning of script' | |
run_return = False | |
msgbox "[script1] enter" | |
msgbox "[script1] param = " & param | |
common_func | |
run_return = True | |
msgbox "[script1] exit" | |
End Sub |
script2.vbs
#$language = "VBScript" | |
#$interface = "1.0" | |
'Scirpt2.vbs can be excuted by itself' | |
Function script2_func () | |
msgbox "[script2][func] this is script2-specific func" | |
End Function | |
Function common_func () | |
msgbox "[script2][common_func] this is common func" | |
End Function | |
param = 200 | |
Sub Main () | |
' notice: reset the run_return value at the beginning of script' | |
run_return = False | |
msgbox "[script2] enter" | |
msgbox "[script2] param = " & param | |
common_func | |
script2_func | |
If run_return = False Then | |
msgbox "[script2] break" | |
Exit Sub | |
End If | |
msgbox "[script2] exit" | |
End Sub |
接下來再使用 run.vbs 這個腳本,一口氣執行 script1 及 script2。
run.vbs
#$language = "VBScript" | |
#$interface = "1.0" | |
' use this to get the output from script1 and script2 ' | |
dim run_return : run_return = False | |
Sub Run(vbsFile) | |
'-------------------------------------------------------------------------' | |
' Read the script text' | |
'-------------------------------------------------------------------------' | |
dim filename : filename = Split(vbsFile, ".")(0) | |
dim fso, f, s | |
set fso = CreateObject("Scripting.FileSystemObject") | |
set f = fso.OpenTextFile(vbsFile) | |
s = f.ReadAll() | |
f.Close | |
'-------------------------------------------------------------------------' | |
' Remove the un-necessary part :' | |
' 1. #$language = "VBScript" ' | |
' 2. #$interface = "1.0"' | |
' ' | |
' Replace the Sub Main () to Sub 'filename' () | |
'-------------------------------------------------------------------------' | |
lines = Split(s, vbCrLf) | |
dim beginLine, endLine | |
For i=0 To Ubound(lines) | |
If InStr(LCase(lines(i)), "language") > 0 and InStr(LCase(lines(i)), "vbscript") Then | |
lines(i) = "" | |
ElseIf InStr(LCase(lines(i)), "interface") > 0 and InStr(LCase(lines(i)), "1.0") Then | |
lines(i) = "" | |
ElseIf InStr(LCase(lines(i)), "sub") > 0 and InStr(LCase(lines(i)), "main") > 0 Then | |
lines(i) = "Sub " & filename & " ()" | |
End If | |
Next | |
'-------------------------------------------------------------------------' | |
' Re-assemble the text ' | |
' Append the execution of 'filename' () in the last line | |
'-------------------------------------------------------------------------' | |
s = "" | |
For Each line In lines | |
s = s & line & vbCrLf | |
Next | |
s = s & vbCrLf | |
s = s & " " & filename | |
'-------------------------------------------------------------------------' | |
' Execute the script ' | |
'-------------------------------------------------------------------------' | |
ExecuteGlobal s | |
End Sub | |
Sub Main () | |
scripts = Array("script1.vbs", "script2.vbs") | |
For Each script In scripts | |
Run script | |
msgbox script & " return value = " & run_return | |
Next | |
End Sub |
以 script1.vbs 來說明
把文字讀進來。去除最前面的兩行:
#$language = "VBScript"
#$interface = "1.0"
接下來把 Sub Main () 改名,變成一個 Sub Script1 () 。最後再多加一行,來呼叫這個名為 Script1 的 Sub 而已。
裡面還有一些別的小技巧,如 function 的使用、及 return value 的用法。
主要是想要達成每個 script 既可以獨立執行、也可以集合在一起,用 run.vbs 來執行的目標。
大致上是這樣,是個說破了就不值錢的小技巧。XD 如果有什麼更好的方法也歡迎告訴我。
沒有留言:
張貼留言