vbs脚本
1、注釋
兩種格式REM +內容或者’+內容
'REM 這是注釋 ' Dim name:name = 2 ' const name2=33 ' Dim msg ' '這也是注釋 ' 'nskdjkasj ' msg="shu"vbs內建函數。inputbox表示輸入。msgbox表示輸出
vbs的函數為一個黑盒,只需要關注輸出和輸入結果即可
2、參數類型
vbs與其他與語言一樣,分為常量和普通變量,常量由const定義,變量由Dim聲明,這兩個聲明方式不一樣。
const name4=33 const name6 =2 Dim name5:name5 = 2 Dim name3,nam23 name3=23聲明的變量只有一種類型variant,下轄多個子類型,根據上下文判斷是數字還是字符串,數字可以進行加減乘數 取余運算
變量子類型
’ 'Empty - 未初始化都是這個值,數值變量,值為0,字符串為""
’ 'Null - 無任何數據的var
’ 'Boolean - true或者false
’ 'Byte - 包含0到255的整數
’ 'Integer - -32768到32768
’ 'Currency - -922337203685477.5808 到 922337203685477.5808
’ 'Long - -2147483648 到 2147483648
’ 'Single - 單精度浮點數,-3402823E38 到 -1.401298E-45(負數),1.401298E-45 到 3402823E38(整數)
’ 'Double - 雙精度浮點數,-1.79769313486232E308 到 -4.94065645841247E-324(負數)
’ 'Date(Time) - 公元100年1月1人到公元9999年12月31日
’ 'String - 可變長字符串,最大長度20億個字符
’ 'Object - 包含對象
’ 'Error - 包含錯誤號
’ '基本每個子類型都有對應的vbs函數進行轉換
’ 'Cbool - 換成布爾型
’ 'Cbyte - 轉成0到255的整數
’ 'Ccure, Cdbl, Csng - 轉成浮點小數,前面那個小數點4位,后面2個更大
’ 'Cdate - 轉換成日期值
’ 'Cint,Clng - 轉成整數,后者范圍比前者大
’ 'Cstr - 轉成字符串
3、選擇語句if和select case
vbs腳本if 語句 if語句采用=而不是==號判斷是否相等
'選擇語句if
Dim a , da=2d=3 if a>d Thenmsgbox a elsemsgbox d End if Dim kl kl = inputbox("putin kl") select case kl case 23msgbox("ss") case 2msgbox "2" case endmsgbox 56 end select4、循環語句
do…loop
'do …loop 跳出循環采用exit do
const pass =“123456”
do a=inputbox("putin")if a=pass thenexit doend ifloopdim i:i=1 do a=a=inputbox("putin")if a=pass thenexit doelsei = i + 1end ifif i = 4 thenexit doend if loopdo while …loop
dim i :i = 1 do while i <4 a=a=inputbox("putin")if a=pass thenexit doelsei = i + 1end ifloop while 放在loop后面減少一次循環dim i:i=1do a=inputbox("putin")if a=pass thenexit doelsei = i + 1end ifloop while i < 4for 循環語句
REm for 循環次數 ...... next,dim kpfor kp=0 to 5msgbox k nextwhile…wend
6、數組,一維數組和二維數組
dim a(9)for i =0 to 9a(i) = imsgbox a(i)next dim name(8),strfor i = 0 to 8 '這里&符號是合并字符串的意思 name(i) = inputbox("putin your number"&i+1) next for i = 0 to 8 msgbox name(i) i=i+1'這里只會打印1,3,5,7,9,因為每次循環多進行了一次i的計算
next
5、函數
vbs自帶函數
vbs常用函數
http://www.zzvips.com/article/90104.html
vbs自定義函數
兩種sub和function
sub沒有返回值,function可以選擇帶有返回值,在自身的functio定義返回值,格式為函數名稱=返回值
Function reboot()xsh.Screen.Synchronous = truexsh.Screen.Send("reboot")xsh.Screen.Send(VbCr)xsh.Session.Sleep(100000)xsh.Screen.Send(VbCr)End function Function login()xsh.Screen.Send(VbCr)xsh.Screen.WaitForString("login")xsh.Screen.Send(VbCr)xsh.Screen.Send("root")xsh.Screen.Send(VbCr)xsh.Session.Sleep(3000)xsh.Screen.Send(VbCr)End function function check(kj )xsh.Screen.Send(" route -ne | grep cellular1 | wc -l ")xsh.Screen.Send(VbCr)screenrow = xsh.Screen.CurrentRowdata = xsh.Screen.Get(screenrow-1,1,screenrow-1,20)if CInt(Instr(1,data,"4",1)) = "1" Thencheck(kj) = -1else check(kj) = kjEnd IfEnd functionFunction error(count0 ,j0)xsh.Screen.Send("check_error cellular1_offline vbs_will_stop:"+CStr(count0))xsh.Screen.Send(VbCr)xsh.Dialog.MsgBox("check_error cellular1 offline vbs_stop: reboot:"+Cstr(count0))xsh.Dialog.MsgBox("check route"+Cstr(j0))End FunctionSub MainDim count:count = 1do while count<3call login()dim j:j=1do while j>0if j=10 Thencall error(count,j)exit doEnd Ifxsh.Screen.Send(" route -ne | grep cellular1 | wc -l ")xsh.Screen.Send(VbCr)screenrow = xsh.Screen.CurrentRowdata = xsh.Screen.Get(screenrow-1,1,screenrow-1,20)if CInt(Instr(1,data,"4",1)) = "2" Thenxsh.Screen.Send("cellular1 online succfess")xsh.Screen.Send(VbCr)exit do End Ifj = j + 1xsh.Session.Sleep(2000)loopif j=10 thenexit doend ifcount = count +1xsh.Session.Sleep(20000)LoopCall reboot() End Sub6、對象
Sub Main'xsh.Session.Open("C:\...\NetSarang\Xshell\Sessions\New Session.xsh")Dim count:count = 1while count > 0xsh.Screen.Synchronous = truexsh.Screen.Send("reboot")xsh.Screen.Send(VbCr)xsh.Session.Sleep(150000)'*** WaitForString ***xsh.Screen.Send(VbCr)xsh.Screen.WaitForString("login")'*** Send ***xsh.Screen.Send("root")xsh.Screen.Send(VbCr)xsh.Session.Sleep(3000)xsh.Screen.Send(VbCr)xsh.Screen.Send(VbCr)'*** 賦值用dim 定義值jkl并同時賦jkl值0*** Dim jkl:jkl = 0'*** while 循環 while開始 Wend結束 ***while jkl = 0'*** vbs像xshell內部發送執行命令 ***xsh.Screen.Send("cat /etc/dhcp3/dhcpd6.leases | grep 2001::5656: |wc -l")'*** 回車,發送一條命令就回車一次,要不不執行***xsh.Screen.Send(VbCr) '*** 等待20s***xsh.Session.Sleep(20000) '*** 使用查找字符串固定條款***screenrow = xsh.Screen.CurrentRow'*** 獲取xshell當前打印內容,這里是讀取其最末行40個字符,第一個參數和第三個參數固定 ***data = xsh.Screen.Get(screenrow-1,1,screenrow-1,20) '*** instr( start_position,string1, string2,nth_appearance ) '***if CInt(Instr(1,data,"1",1)) = "1" Thenjkl = 1End If'***if 語句格式 if 判斷條件 Then 有else就加else 沒有就End If***Wendscreenrow = xsh.Screen.CurrentRowdata = xsh.Screen.Get(screenrow-1,1,screenrow-1,20)if CInt(Instr(1,data,"1",1)) = "0" Thenxsh.Screen.Send("cat /etc/dhcp3/dhcpd6.leases | grep 2001::5656: |wc -l")xsh.Screen.Send(VbCr)xsh.Session.Sleep(40000)screenrow = xsh.Screen.CurrentRowdata = xsh.Screen.Get(screenrow-1,1,screenrow-1,20)if CInt(Instr(1,data,"1",1)) = "0" Thenxsh.Screen.Send("check_error:" + data)xsh.Screen.Send(VbCr)'***xshell外部日志提醒,不點擊就暫停執行***xsh.Dialog.MsgBox("check_error:" + data)count = 0End IfEnd Ifxsh.Screen.Send("count=" + cstr(count))count = count + 1xsh.Screen.Send(VbCr)Wend End Sub Sub Main'xsh.Session.Open("C:\...\NetSarang\Xshell\Sessions\New Session.xsh")Dim count:count = 0while count < 50xsh.Screen.Synchronous = truexsh.Screen.Send("reboot")xsh.Screen.Send(VbCr)xsh.Session.Sleep(120000)xsh.Screen.Send(VbCr)xsh.Screen.Send(VbCr)xsh.Screen.Send(VbCr)xsh.Screen.WaitForString("login")xsh.Screen.Send(VbCr)xsh.Screen.Send("root")xsh.Screen.Send(VbCr)xsh.Session.Sleep(3000)xsh.Screen.Send(VbCr)Dim j:j = 1do while j > 0xsh.Screen.Send(" route -ne | grep 0.0.0.0 | wc -l ")xsh.Screen.Send(VbCr)screenrow = xsh.Screen.CurrentRowdata = xsh.Screen.Get(screenrow-1,1,screenrow-1,20) if CInt(Instr(1,data,"4",1)) = "1" Thenexit doEnd Ifj = j + 1if j = 20 Thenxsh.Screen.Send("check_error:" + count)xsh.Screen.Send(VbCr)xsh.Dialog.MsgBox("reboot:" +count)xsh.Dialog.MsgBox("check route" +j)End Ifxsh.Session.Sleep(15000)Loopxsh.Session.Sleep(30000)count = count + 1Wend End Sub總結