一个在Windows下的ping脚本(使用WMI 的Win32_PingStatus 实现)

  非常不喜欢windows自带Ping工具,慢吞吞不说结果也很不精确,非常不适合用来排查网络故障,尤其是
对我这种急性子的人来说,更不能接受(有时候是比较急啦),于是就偷懒写了以下这个东西,希望对大家有用:


set  objArgs  =  wscript.Arguments 


Dim  PacketReceived , PacketLost , MinimumTime , MaximumTime , AverageTime ,TimeSum , tmp

PacketReceived 
=   0
PacketLost     
=   0
MinimumTime    
=   2000
MaximumTime    
=   0  
AverageTime    
=   0
TimeSum        
=   0

WScript.Echo 
""
WScript.Echo 
" Pinging  " & objArgs( 0 ) & "  with 32 bytes of data: "
WScript.Echo 
""


For  i  =   0   To  objArgs( 1 ) - 1

    
Set  objWMIService  =   GetObject ( " winmgmts:\\.\root\cimv2 " )
    
Set  colItems  =  objWMIService.ExecQuery _
        (
" Select * from Win32_PingStatus  "   &   " Where Address = ' "   &  objArgs( 0 &   " ' " )
    
For   Each  objItem in colItems
    
        
If  objItem.StatusCode  =   0   Then  
            
            TimeSum 
=  TimeSum  +  objItem.ResponseTime
            PacketReceived 
=  PacketReceived  +   1
            WScript.Echo 
" Reply from  " & objArgs( 0 ) & " : bytes= " & objItem.ReplySize & "  time= " & objItem.ResponseTime & " ms TTL= " & objItem.ResponseTimeToLive

            
If  objItem.ResponseTime  >  MaximumTime  Then  
                MaximumTime 
=  objItem.ResponseTime
            
End   If  
        
            
If  objItem.ResponseTime  <  MinimumTime  Then  
                MinimumTime 
=  objItem.ResponseTime
            
End   If  
            
                
        
Else  
            
            WScript.Echo 
" Request timed out. "
            PacketLost 
=  PacketLost  +   1
                
        
End   If   
        
    
Next  
Next  


WScript.Echo 
""


If   PacketReceived  =   0    Then  

    WScript.Echo 
" Ping statistics for  " & objArgs( 0 ) & " : "
    WScript.Echo     
" Packets: Sent =  " & objArgs( 1 ) & " , Received = 0, Lost =  " & objArgs( 1 ) & "  (100% loss), "


Else  

    tmp 
=   Round (PacketLost / objArgs( 1 ), 3 ) * 100
    
    
If   Left (tmp ,  1 =   " . "   Then  
        tmp 
=   " 0 " & tmp
    
End   If  
    
    WScript.Echo 
" Ping statistics for  " & objArgs( 0 ) & " : "
    WScript.Echo    
" Packets: Sent =  " & objArgs( 1 ) & " , Received =  " & PacketReceived & " , Lost =  " & PacketLost & "  ( " & tmp & " % loss), "
    WScript.Echo 
" Approximate round trip times in milli-seconds: "
    WScript.Echo    
" Minimum =  " & MinimumTime & " ms, Maximum =  " & MaximumTime & " ms, Average =  " & Round (TimeSum / (PacketReceived), 0 ) & " ms "


End   If  








 写得很粗糙,目前只支持2个参数,目的IP及Ping包数。等以后发大财了再完善一下,还有PowerShell 及Bash版本等...
    
 运行方法很简单,把以上代码保存为.vbs文件,通过命令行运行cscript *.vbs 即可,下面例子可供参考:


E:\VBS\TS>cscript arg.vbs 192.168.1.1 2
Microsoft (R) Windows Script Host Version 5.7
版权所有(C) Microsoft Corporation 1996-2001。保留所有权利。


Pinging 192.168.1.1 with 32 bytes of data:

Reply from 192.168.1.1: bytes=32 time=132ms TTL=64
Reply from 192.168.1.1: bytes=32 time=44ms TTL=64

Ping statistics for 192.168.1.1:
Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 44ms, Maximum = 132ms, Average = 88ms

E:\VBS\TS>cscript arg.vbs 192.168.1.2 2
Microsoft (R) Windows Script Host Version 5.7
版权所有(C) Microsoft Corporation 1996-2001。保留所有权利。


Pinging 192.168.1.2 with 32 bytes of data:

Request timed out.
Request timed out.

Ping statistics for 192.168.1.2:
Packets: Sent = 2, Received = 0, Lost = 2 (100% loss),

E:\VBS\TS>



转载于:https://www.cnblogs.com/mrwsr/articles/1238518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值