vbs 记录

最近想尝试在启动tailscale 后,获取tailscale的邻居ip ,并发起ping 请求。这样做的目的也只是为了,不想再 登录向日葵,发起 ping 请求,并完成打洞。事实上并不需要这样,应该是自动完成的打洞互联的。但还是说一下使用vbs 完成这一系列的操作,虽然没完成完,把已经完成的功能简要说一下, 算是总结。

1,使用vbs ,创建一个shell 对象,调用这个 shell 对象 执行cmd 命令。执行时 需要 在cmd 命令前添加 %comspec%  /

Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec ("%comspec% /c  tailscale status ")

2,获取命令行执行的结果,并赋值给变量

Do While Not objExecObject.StdOut.AtEndOfStream
	strText = objExecObject.StdOut.ReadAll()
loop

3,取出特定的字符,比如ip;先创建正则表达式对象,并设置该对象的模式,以及全局属性(进行多次匹配),调用 表达式对象的执行方法,输入要匹配的内容(字符串变量)

Set objRegExp=New RegExp

objRegExp.pattern="fd7a:115c:a1e0::\d"
objRegExp.pattern=".*active"
objRegExp.Global = True
Set colMatches=objRegExp.Execute(strText)

4,匹配的结果也是一个对象, 获取匹配到的结果数量并输出,特定的内容,这里使用msgbox,将匹配到的内容逐个获取,并将获取到的内容作为参数再次传入正则对象,这里的正则对象的模式不一样,这里是一个ipv6的地址,赋值给变量ip。

msgbox  colMatches.Count
objRegExp.pattern="fd7a:115c:a1e0::\d"
If colMatches.Count > 0 Then
    For Each match in colMatches
        Set colMatches2=objRegExp.Execute(match.value)
	    If colMatches2.Count > 0 Then
	       For Each match2 in colMatches2
            
           WScript.Echo TypeName(match2.value)
	       ip= match2.value
           Next
        End If
    Next
End If

5,将取到的结果,作为参数执行 ping 命令,并将执行结果 ,显示出来。(这里没有去判断 执行结果是否成功,都成功就退出,如果不成功,就隔多久再次ping ,直到 成功)

Set objExecObject2 = objShell.Exec ("%comspec% /c  ping  " & ip)

Do While Not objExecObject2.StdOut.AtEndOfStream
	strText2 = objExecObject2.StdOut.ReadAll()
loop

msgbox strText2

6,尝试获取命令的执行结果,如果ping 通了,就退出,如果没有ping 通 就一直ping 并 间隔几秒,并将命令的执行结果都弹窗出来(成功与否)

Set objShell = CreateObject("WScript.Shell")
sum =0
While sum <=0
    Set objExecObject = objShell.Exec ("%comspec% /c  ping  192.6.7.5 ")
    Do While Not objExecObject.StdOut.AtEndOfStream
	strText = objExecObject.StdOut.ReadAll()
    loop
    msgbox strText    
    If objExecObject.ExitCode =0 Then
        WScript.Echo "ping tong le"
        
        sum=1
    Else
      
	    WScript.Echo "mei ping tong"
        WScript.Sleep 5000
    End If
Wend

7,封装到函数里 使用Function 定义函数

Function pingip(ByRef objShell,ByVal ip)
    sum =0
    While sum <=0
        Set objExecObject = objShell.Exec ("%comspec% /c  ping   " & ip )
        Do While Not objExecObject.StdOut.AtEndOfStream
	    strText = objExecObject.StdOut.ReadAll()
        loop
        msgbox strText    
        If objExecObject.ExitCode =0 Then
            WScript.Echo "ping tong le"
        
            sum=1
        Else
      
	    WScript.Echo "mei ping tong"
            WScript.Sleep 5000
        End If
    Wend

End Function


Set objShell = CreateObject("WScript.Shell")
Dim ip
ip = "192.168.4.5"
a=pingip(objShell,ip)

8,使用Sub 封装 函数

Sub pingip(ByRef objShell,ByVal ip)
    sum =0
    While sum <=0
        Set objExecObject = objShell.Exec ("%comspec% /c  ping   " & ip )
        Do While Not objExecObject.StdOut.AtEndOfStream
	    strText = objExecObject.StdOut.ReadAll()
        loop
        msgbox strText    
        If objExecObject.ExitCode =0 Then
            WScript.Echo "ping tong le"
        
            sum=1
        Else
      
	    WScript.Echo "mei ping tong"
            WScript.Sleep 5000
        End If
    Wend

End Sub


Set objShell = CreateObject("WScript.Shell")
Dim ip
ip = "8.8.8.8"
pingip objShell , ip

第一次完整的脚本

Dim  ip
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec ("%comspec% /c  tailscale status ")

Do While Not objExecObject.StdOut.AtEndOfStream
	strText = objExecObject.StdOut.ReadAll()
loop

Set objRegExp=New RegExp

objRegExp.pattern="fd7a:115c:a1e0::\d"
objRegExp.pattern=".*active"
objRegExp.Global = True
Set colMatches=objRegExp.Execute(strText)
msgbox  colMatches.Count
objRegExp.pattern="fd7a:115c:a1e0::\d"
If colMatches.Count > 0 Then
    For Each match in colMatches
        Set colMatches2=objRegExp.Execute(match.value)
	    If colMatches2.Count > 0 Then
	        For Each match2 in colMatches2
            
               WScript.Echo TypeName(match2.value)
	           ip= match2.value
            Next
        End If
    Next
End If


Set objExecObject2 = objShell.Exec ("%comspec% /c  ping  " & ip)

Do While Not objExecObject2.StdOut.AtEndOfStream
	strText2 = objExecObject2.StdOut.ReadAll()
loop

msgbox strText2

第二次完整脚本(到目前为止基本上已经完成了, 虽然写了循环,循环各个 ip,并 循环ping 各个没有ping 通的ip,但仍有可能并不完善, 如果一共有3个ip, ping 第一个ip 时,就ping 不通,在函数内部循环,会不会阻塞外层循环 第二个ip 的执行。有待测试)

Sub pingip(ByRef objShell,ByVal ip)
    sum =0
    While sum <=0
        Set objExecObject = objShell.Exec ("%comspec% /c  ping   " & ip )
        Do While Not objExecObject.StdOut.AtEndOfStream
	    strText = objExecObject.StdOut.ReadAll()
        loop
        msgbox strText    
        If objExecObject.ExitCode =0 Then
            WScript.Echo "ping tong le"
        
            sum=1
        Else
      
	    WScript.Echo "mei ping tong"
            WScript.Sleep 5000
        End If
    Wend

End Sub


Set objShell = CreateObject("WScript.Shell")

Set objExecObject = objShell.Exec ("%comspec% /c  tailscale status ")

Do While Not objExecObject.StdOut.AtEndOfStream
	strText = objExecObject.StdOut.ReadAll()
loop

Set objRegExp=New RegExp

objRegExp.pattern="fd7a:115c:a1e0::\d"
objRegExp.pattern=".*active"
objRegExp.Global = True
Set colMatches=objRegExp.Execute(strText)
msgbox  colMatches.Count
objRegExp.pattern="fd7a:115c:a1e0::\d"
If colMatches.Count > 0 Then
    For Each match in colMatches
        Set colMatches2=objRegExp.Execute(match.value)
	If colMatches2.Count > 0 Then
	   For Each match2 in colMatches2
            
           WScript.Echo TypeName(match2.value)
	       ip= match2.value
           pingip objShell,ip
           Next
        End If
    Next
End If

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值