powershell 改进版的tracert&mtr$tracroute
来源:互联网 发布:c windows tasks 编辑:程序博客网 时间:2024/04/29 09:10
#本来想写个类似于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.ttlif($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.arraylist1..$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=01..$counts|%{$time=(get-date)$Receive=$ping.Send($daddr,$timeout,$buffer,$options)if("Success","TtlExpired" -contains $Receive.Status){[int]$RoundtripTime=((get-date)-$time).TotalMilliseconds$total+=$RoundtripTimeif($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.stepif($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.arraylistforeach($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.outputif($thread.output[0].Status -eq "Success"){break}})|ogv -Title "tracroute..."$steps=$thread.output[0].stepforeach($thread in $traclists){$thread.thread.Runspace.Close()}$fillendtime=get-date"$fillendtime 完成traceroute $steps`跳 耗时$(($fillendtime-$starttime).TotalSeconds)"
0 0
- powershell 改进版的tracert&mtr$tracroute
- tracert、traceroute、mtr、WinMTR
- Azure虚拟机中使用Tracert/Traceroute/MTR(My Traceroute)的原理讨论
- traceroute /mtr 的学习
- mtr
- mtr
- Tracert的使用
- linux下的tracert
- tracert的使用!
- tracert
- tracert
- tracert
- tracert
- tracert
- tracert
- tracert
- tracert 很慢的问题
- 统计对象在PowerShell 3.0中的改进
- GTest的环境搭建与导入dll测试
- 各种优秀前端网站 ++++ 各种优秀前端组件
- PHP开发不能违背的安全规则 过滤用户输入
- eclipse中设置字体大小
- Hibernate (1) 多对多查询
- powershell 改进版的tracert&mtr$tracroute
- 【C++】数组类的定义(类模板)
- Android 引用外部项目库笔记
- AsyncTask和Thread+Handler对比
- 微信隐藏代码功能大汇总-你不知道的微信命令行.doc
- 第16周-阅读程序4(3)
- IOS 开发过程中的 消息通知 小红点
- struts2实现文件上传(类型、大小过滤——手动、拦截器)
- 移动端浏览器类型辨别