1. 苏葳的备忘录首页
  2. 编程

Windows 7下测试网络连通性并记录日志的vbs脚本

vbscript ping经常调试网络的人都会知道,网络通或不通都好处理,但是碰到网络时通时断的情况却颇为头疼。前段时间自己遇到了这种情况。无法判断是单位网络不稳定,网络管理策略变动,网线接头故障,还是本机的某些程序影响,因此写了如下vbs脚本,打算在晚上下班后长时间运行,以分析出网络中断的规律。此脚本运行后,会自动在网络不通时把时间写入日志文件,方便之后查看分析。

使用命令格式:testping.vbs  ip或网址  日志名  间隔秒数 即可。注意在网络不通时,日志记录会5秒一跳,即把间隔时间设为1秒时,日志中还会是5秒写一次,设为2时,是6秒写一次。跟用系统ping 命令测试地址时是一样的。

'win7用
' testping.vbs www.163.com  163.log 30    //testping.vbs 网址/ip  日志名   间隔秒数
Option Explicit
dim wshshell,a,args
Set args = WScript.Arguments
set wshshell=createobject("scripting.filesystemobject")
set a=wshshell.createtextfile(WScript.Arguments(1))
a.writeline "       dest:"+WScript.Arguments(0)
a.writeline "    logname:"+WScript.Arguments(1)
a.writeline "interval(s):"+WScript.Arguments(2)
do
If Ping(WScript.Arguments(0))=True Then
   'a.write "ERR at:"
   'a.writeline now
Else
   a.write "ERR at:"
   a.writeline now
End If
WScript.sleep WScript.Arguments(2)*1000
loop
a.close
Function Ping(sHost)
  dim objPing,objRetStatus
  Set objPing=GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select StatusCode from Win32_PingStatus where address='"&sHost&"'")
  For Each objRetStatus in objPing
    If objRetStatus.StatusCode<>0 Then
      Ping=False
    Else
      Ping=True
    End if
  Next
End Function

中断呢,就只能在任务管理器时结束掉wscript脚本解释器了。

原创文章,作者:苏葳,如需转载,请注明出处:https://www.swmemo.com/573.html

发表评论

邮箱地址不会被公开。 必填项已用*标注