试试powershell
----------保存为trace-route2.ps1------------- function Trace-Route
- {
- param(
- # The URL to trace
- [Parameter(Mandatory=$true)]
- [Uri]$Url,
- # The timeout for the request, in milliseconds
- [Timespan]$Timeout = "0:0:0.25",
- # The maximum number of hops for the trace route
- [Int]$MaximumHops = 32
- )
-
- process {
- Invoke-Expression "tracert -w $($timeOut.TotalMilliseconds) -h $MaximumHops $url" |
- Where-Object {
- if ($_ -match "[.+]") {
- $destination
- try {
- $destination = [IpAddress]$_.Split("[]",[StringSplitOptions]"RemoveEmptyEntries")[-1]
- } catch {
- return $false
- }
- }
- $true
- } |
- Where-Object {
- if ($_ -like "*Request timed out.*") {
- throw "Request timed Out"
- }
- return $true
- } |
- Foreach-Object {
- if ($_ -like "*ms*" ) {
- $chunks = $_ -split " " | Where-Object { $_ }
- $destAndip = $chunks[-1]
- $dest, $ip = $destAndip.Replace("[", "").Replace("]","") -split " "
-
- if (-not $ip) {
- $ip = $dest
- $dest = ""
- }
-
- $ip = @($ip)[0].Trim() -as [IPAddress]
-
-
- if ($chunks[1] -eq '*' -and $chunks[2] -eq '*' -and $chunks[3] -eq '*') {
- Write-Error "Request Timed Out"
- return
- }
- $trace = New-Object Object
- $time1 = try { [Timespan]::FromMilliseconds($chunks[1].Replace("<","").Replace(" ms", ""))} catch {}
- $time2 = try { [Timespan]::FromMilliseconds($chunks[1].Replace("<","").Replace(" ms", ""))} catch {}
- $time3 = try { [Timespan]::FromMilliseconds($chunks[1].Replace("<","").Replace(" ms", ""))} catch {}
- $trace |
- Add-Member NoteProperty HopNumber ($chunks[0].Trim() -as [uint32]) -PassThru |
- Add-Member NoteProperty Time1 $time1 -PassThru |
- Add-Member NoteProperty Time2 $time2 -PassThru |
- Add-Member NoteProperty Time3 $time3 -PassThru |
- Add-Member NoteProperty Ip $ip -PassThru |
- Add-Member NoteProperty Host $dest -PassThru |
- Add-Member NoteProperty DestinationUrl $url -PassThru |
- Add-Member NoteProperty DestinationIP $destination -PassThru
-
- }
- }
- }
- }
-
-
- Trace-Route $args[0]
复制代码 ---------------保存为 test.ps1---------------- $script:脚本存储路径 = Split-Path -Parent $myinvocation.mycommand.path
- $Env:Path+=";$script:脚本存储路径;"
- $sapi = New-Object -COM Sapi.SpVoice
-
-
- $tracert结果 = (e:\abc\trace-route2.ps1 www.163.com).ip.ipaddresstostring # 你文档保存的路径要指对
- write-host "返回ip为:`n"
- $tracert结果
-
- if ( $tracert结果 -contains "123.125.34.30") # 这里写你要判断的ip
- {
- $sapi.Speak("报告局长!联通线路通信正常")
- }
- else
- {
- $sapi.Speak("报告处长!联通线路中断且电信线路正常")
- }
复制代码 在powershell中运行这个test.ps1 |