码迷,mamicode.com
首页 > 系统相关 > 详细

Powershell追踪路由

时间:2018-10-31 12:42:52      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:power   情况   ping   追踪   路由   reply   bre   ngoptions   cert   

一般情况下,我们可以通过Cmdlet命令来实现路由追踪

技术分享图片

我们是否能尝试通过Powershell完成此功能呢?

 

脚本具体如下,可以直接粘贴

function GetTraceRoute($hostname) {
function __TracerRouteResult($Address,$Status, $TTL) {
[PSCustomObject]@{Address=$Address;Status=$Status;TTL=$TTL}
}

$timeout = 10000;
$maxTTL = 30;
$bufferSize = 32;
# ---------------------------------------------

$buffer = [byte[]]@(1..32)
[Random]::new().NextBytes($buffer)
$pinger = [System.Net.NetworkInformation.Ping]::new()

for ($ttl = 1; $ttl -le $maxTTL; $ttl++) {
$options = [System.Net.NetworkInformation.PingOptions]::new($ttl, $true)
$reply = $pinger.Send($hostname, $timeout, $buffer, $options)

if ($reply.Status -eq [System.Net.NetworkInformation.IPStatus]::Success) {
# Success means the tracert has completed
Write-Output (__TracerRouteResult $reply.Address ([System.Net.NetworkInformation.IPStatus]::Success) $ttl)
return
} elseif ($reply.Status -eq [System.Net.NetworkInformation.IPStatus]::TtlExpired) {
# TtlExpired means we‘ve found an address, but there are more addresses
Write-Output (__TracerRouteResult $reply.Address ([System.Net.NetworkInformation.IPStatus]::TtlExpired) $ttl)
} elseif ($reply.Status -eq [System.Net.NetworkInformation.IPStatus]::TimedOut) {
Write-Output (__TracerRouteResult ‘‘ ([System.Net.NetworkInformation.IPStatus]::TimedOut) $ttl)
} else {
break
}
}
}

 

Powershell追踪路由

标签:power   情况   ping   追踪   路由   reply   bre   ngoptions   cert   

原文地址:https://www.cnblogs.com/zhr1217/p/9882142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!