;初始化数组 pinginfo := Object() ;建立函数 ping(pinginfo){ site := pinginfo["site"] ;赋值 RunWait, %ComSpec% /c ping -n 1 %site% >%A_Temp%\ahk_ping.tmp, , Hide ;有cmd ping网址并保存到临时文件夹 FileRead, content, %A_Temp%\ahk_ping.tmp ;读取整个文件到变量 StringReplace, content, content, `r, , All ;去除回车符 StringSplit, var, content, `n ;按换行分割 If content Contains 请求超时,Request timed out pinginfo["msg"] = "请求超时" ;返回msg If content Contains 找不到主机,could not find host pinginfo["msg"] = "找不到主机" Else { pinginfo["ip"] := RegExReplace(var3, "(来自|Reply from) (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\s的回复|]*: (字节|bytes)=\d{1,3}\ (时间|time)[=<]\d{1,3}ms TTL=\d{1,3}","$2") ;正则替换 pinginfo["time"] := RegExReplace(var3, "(来自|Reply from) \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}[\s的回复|]*: (字节|bytes)=\d{1,3}\ (时间|time)[=<](\d{1,3})ms TTL=\d{1,3}","$4") pinginfo["ttl"] := RegExReplace(var3, "(来自|Reply from) \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}[\s的回复|]*: (字节|bytes)=\d{1,3}\ (时间|time)[=<]\d{1,3}ms TTL=(\d{1,3})","$4") pinginfo["msg"] = "正常" } Return pinginfo ;返回数组 } ;调用方法 pinginfo["site"] := "www.baidu.com" ;赋值网址 pinginfo := ping(pinginfo) ;调用函数并保存到pinginfo ip := pinginfo["ip"] ;取出 time := pinginfo["time"] site := pinginfo["site"] MsgBox, 网址:%site%(%ip%) 的ping延时为%time%ms ;测试
49 queries in 1.330 seconds |