欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vbs脚本

程序员文章站 2022-06-26 14:34:25
...

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 , d
 a=2
 d=3
if a>d Then
    msgbox a
else
  msgbox d
End if
Dim kl
kl = inputbox("putin kl")
select case kl
case 23
 msgbox("ss")
case 2
 msgbox "2"
case end
 msgbox 56
end select

4、循环语句

do…loop

'do …loop 跳出循环采用exit do

const pass =“123456”

do 
    a=inputbox("putin")
    if a=pass then
        exit do
        end if
    
loop

dim i:i=1
do 
        a=a=inputbox("putin")
        if a=pass then
            exit do
                else
                i = i + 1
            end if

            if i = 4 then
                exit do
                end if
loop

do while …loop

dim i :i = 1
do while i <4
a=a=inputbox("putin")
 if a=pass then
 exit do
 else
 i = i + 1
 end if
 loop
while 放在loop后面减少一次循环
 dim i:i=1
 do 
  a=inputbox("putin")
  if a=pass then
     exit do
  else
     i = i + 1
  end if
 loop while i < 4

for 循环语句

REm for 循环次数 ...... next,

 dim kp
 for kp=0 to 5
 msgbox k 
next

while…wend

6、数组,一维数组和二维数组

 dim a(9)
 for i =0 to 9
 a(i) = i
 msgbox a(i)
 next
dim name(8),str

for 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 = true
	    xsh.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.CurrentRow
        data = xsh.Screen.Get(screenrow-1,1,screenrow-1,20)
		
		if CInt(Instr(1,data,"4",1)) = "1"  Then
            check(kj) = -1
		else 
		    check(kj) = kj
        End If
   End function
   
   
   Function 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 Function
   
Sub Main
   Dim count:count = 1
   do while count<3
		 call login()
		 dim j:j=1
		 do while j>0
		      if  j=10 Then
			       call error(count,j)
				   exit do
              End If
			  
              xsh.Screen.Send(" route  -ne | grep cellular1 | wc -l ")
		      xsh.Screen.Send(VbCr)
              screenrow = xsh.Screen.CurrentRow
              data = xsh.Screen.Get(screenrow-1,1,screenrow-1,20)
		
		      if CInt(Instr(1,data,"4",1)) = "2"  Then
			       xsh.Screen.Send("cellular1 online succfess")
				   xsh.Screen.Send(VbCr)
			       exit do 				   
              End If
			 
			 j = j + 1
			 xsh.Session.Sleep(2000)
		 loop
		 if j=10 then
		    exit do
		 end if
		 count = count +1
		 xsh.Session.Sleep(20000)
   Loop
   Call reboot()
End Sub

6、对象

Sub Main
	'xsh.Session.Open("C:\...\NetSarang\Xshell\Sessions\New Session.xsh")
	Dim count:count = 1
	while count > 0
		xsh.Screen.Synchronous = true
		
		xsh.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" Then
                  jkl = 1
              End If
                '***if 语句格式 if  判断条件 Thenelse就加else 没有就End If***
              Wend


		screenrow = xsh.Screen.CurrentRow
		data = xsh.Screen.Get(screenrow-1,1,screenrow-1,20)
		if CInt(Instr(1,data,"1",1)) = "0" Then
			xsh.Screen.Send("cat /etc/dhcp3/dhcpd6.leases | grep 2001::5656: |wc -l")
			xsh.Screen.Send(VbCr)
		
			xsh.Session.Sleep(40000)
			screenrow = xsh.Screen.CurrentRow
			data = xsh.Screen.Get(screenrow-1,1,screenrow-1,20)
			if CInt(Instr(1,data,"1",1)) = "0" Then
				xsh.Screen.Send("check_error:" + data)
				xsh.Screen.Send(VbCr)
                                '***xshell外部日志提醒,不点击就暂停执行***
				xsh.Dialog.MsgBox("check_error:" + data)
				count = 0
			End If
		End If
		
		xsh.Screen.Send("count=" + cstr(count))
		count = count + 1
		xsh.Screen.Send(VbCr)
	Wend
End Sub
Sub Main
	'xsh.Session.Open("C:\...\NetSarang\Xshell\Sessions\New Session.xsh")
	Dim count:count = 0
	while count < 50
        xsh.Screen.Synchronous = true

	    xsh.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 = 1
        do while j > 0
				xsh.Screen.Send(" route  -ne | grep 0.0.0.0 | wc -l ")
		        xsh.Screen.Send(VbCr)
                screenrow = xsh.Screen.CurrentRow
                data = xsh.Screen.Get(screenrow-1,1,screenrow-1,20) 
		        if CInt(Instr(1,data,"4",1)) = "1"  Then
                        exit do
                End If
				
				j = j + 1
				
				if  j = 20 Then
                   xsh.Screen.Send("check_error:" + count)
		           xsh.Screen.Send(VbCr)
                   xsh.Dialog.MsgBox("reboot:" +count)
                   xsh.Dialog.MsgBox("check route" +j)
                End If
    
				xsh.Session.Sleep(15000)
	    Loop
		
	    xsh.Session.Sleep(30000)
        count = count + 1
 	Wend
End Sub