powershell 改进版的tracert&mtr$tracroute

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shrekz/article/details/51701610
#本来想写个类似于linuxde mtr的软件,但动态刷新需要些界面,又不想写控制台刷新。做了个简版的,一般就用来测试网络质量和探查问题,也够用了。
#qqway是一个地址查询库,大家可以自己去找度娘。
#param($dm)
$starttime=get-date
"$starttime 开始traceroute……"
#初始化
$dm="www.baidu.com"
$timeout=1000 #设置超时的时间
$counts=10 #设置测试次数
$maxstep=30 #设置最大跳数

#测量到达跳数
$teststep=New-Object System.Net.NetworkInformation.Ping
$rece=$teststep.send($dm)
if($rece.Status -eq "Success"){
$ttl=$rece.Options.ttl
if($ttl -gt 64){
$steps=129-$ttl
}else{
$steps=65-$ttl}
}else{$steps=$maxstep}
$QQWrypath="C:\xxxx\xxxx\" #设置qqway安装路径
$null=[system.reflection.Assembly]::LoadFrom($QQWrypath+"QQWry.NET.dll")
$traclist=new-object collections.arraylist
1..$steps|%{
$teac=new-object psobject|select step,buffer,PingOptions,ping,QQWry
$teac.step=$_
$teac.buffer=[text.encoding]::Default.getbytes("abcdefghijklmnopqrstuvwabcdefghi")
$teac.PingOptions=new-object System.Net.NetworkInformation.PingOptions($_,1)
$teac.ping=New-Object System.Net.NetworkInformation.Ping
$teac.QQWry=New-Object QQWry.NET.QQWryLocator($QQWrypath+"QQWry.Dat")
$null=$traclist.add($teac)}

#tracroute脚本
$getping={param($daddr,$timeout,$counts,$object)
$time=(get-date)
$ping = $object.ping
$buffer=$object.buffer
$options=$object.PingOptions
$rst=new-object psobject|select step,ip,loos%,count,max,min,avg,loss,Country,Local,Status
$Address=$null
$total=0
1..$counts|%{
$time=(get-date)
$Receive=$ping.Send($daddr,$timeout,$buffer,$options)
if("Success","TtlExpired" -contains $Receive.Status){
[int]$RoundtripTime=((get-date)-$time).TotalMilliseconds
$total+=$RoundtripTime
if($rst.count){$rst.count+=1}else{$rst.count=1}
if("Success","TtlExpired" -notcontains $rst.Status){$rst.Status=$Receive.Status}
if(!$rst.max -or $RoundtripTime -gt $rst.max){$rst.max=$RoundtripTime}
if(!$rst.min -or $RoundtripTime -lt $rst.min){$rst.min=$RoundtripTime}
if(!$Address){$rst.ip=$Receive.Address}
}
}

$rst.step=$object.step
if($rst.ip){
$Query=$object.QQWry.Query($rst.ip)
$rst.Country=$Query.Country 
$rst.Local=$Query.Local} 
$rst.loss=$counts-$rst.count
$rst."loos%"=($rst.loss/$counts).tostring("P0")
$rst.avg=[int]($total/$rst.count)
$rst
}

#创建多线程测试任务
$traclists=new-object collections.arraylist
foreach($trac in $traclist){
    $gpc=[powershell]::Create()
    $gpc.Runspace=[System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
    $gpc.Runspace.ThreadOptions="UseNewThread"
    $gpc.Runspace.ApartmentState="STA"
    $gpc.Runspace.open()
    [void]$gpc.AddScript($getping)
    [void]$gpc.AddParameter("daddr",$dm)
    [void]$gpc.AddParameter("timeout",$timeout)
    [void]$gpc.AddParameter("counts",$counts)
    [void]$gpc.AddParameter("object",$trac)
    $AsyncResult=$gpc.BeginInvoke()
    $result=New-Object psobject|select output,result,thread
    $result.output=$null
    $result.result=$AsyncResult
    $result.thread=$gpc
    [void]$traclists.add($result)
     }

#回收任务并展示
$(foreach($thread in $traclists){
$thread.output=$thread.thread.EndInvoke($thread.result)
$thread.output
if($thread.output[0].Status -eq "Success"){break}
})|ogv -Title "tracroute..."
$steps=$thread.output[0].step
foreach($thread in $traclists){
$thread.thread.Runspace.Close()
}
$fillendtime=get-date
"$fillendtime 完成traceroute $steps`跳 耗时$(($fillendtime-$starttime).TotalSeconds)"

猜你喜欢

转载自blog.csdn.net/shrekz/article/details/51701610