Isz*_*szi 7 networking windows-8.1 powershell-4.0
在 Windows 8.1 中,Test-NetConnectioncmdlet 可用于检查远程系统上网络端口的状态。但是,有时它可能会不必要地慢。我想知道是否有一些我可以调整的选项,或者我可以使用的替代 PowerShell 命令,以使这个过程更快。
Test-NetConnection如果远程系统没有响应,可能需要大约 10 秒才能返回结果。每当指定端口时,它都会运行两个连接测试,每个测试大约需要 5 秒才能超时。第一个测试是基本的 ICMP 回声检查。如果系统离线,或者如果系统(或任何干预基础设施)被配置为阻止或不响应 ICMP 回显请求,这将超时。第二个测试是对指定端口的实际检查。如果系统离线,或者路径上有防火墙阻塞端口,这将超时。
在我当前的用例中,远程系统在可靠的千兆以太网连接上只有两跳。因此,任何请求的 5 秒超时都非常过分——我可能仍然可以在 30 毫秒或更短的超时时间内获得可靠的结果!此外,已知该系统对 ICMP 回声无响应,即使它可能在线并提供所有其他服务。因此,如果我可以完全不用 ICMP 回声测试,并减少 TCP 连接测试的超时时间,以加快Test-NetConnection用于此目的的脚本,那就太好了。
是否Test-NetConnection可以选择改变这些行为?(我已经阅读了详细的帮助文件,答案似乎是否定的 - 但我很高兴被告知我遗漏了一些东西。)或者还有另一种方法可以使用 PowerShell 运行相同的检查,但更快?
出于各种原因,我更喜欢将我的脚本限制为尽可能使用操作系统内置的功能。假设环境是 Windows 8.1 的全新版本,应用了所有适当的 Windows 更新,并且第三方工具不是一个选项。
非常基本的(超时 100 毫秒):
function testport ($hostname='yahoo.com',$port=80,$timeout=100) {
$requestCallback = $state = $null
$client = New-Object System.Net.Sockets.TcpClient
$beginConnect = $client.BeginConnect($hostname,$port,$requestCallback,$state)
Start-Sleep -milli $timeOut
if ($client.Connected) { $open = $true } else { $open = $false }
$client.Close()
[pscustomobject]@{hostname=$hostname;port=$port;open=$open}
}
testport
hostname port open
-------- ---- ----
yahoo.com 80 True
Run Code Online (Sandbox Code Playgroud)
您可以使用它来测试连接 -取自PowerShell 代码存储库(作者“BSonPosh”):
“Test-Port 创建到指定端口的 TCP 连接。默认情况下,它连接到端口 135,超时为 3 秒。”
Param([string]$srv,$port=135,$timeout=3000,[switch]$verbose)
# Test-Port.ps1
# Does a TCP connection on specified port (135 by default)
$ErrorActionPreference = "SilentlyContinue"
# Create TCP Client
$tcpclient = new-Object system.Net.Sockets.TcpClient
# Tell TCP Client to connect to machine on Port
$iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
# Set the wait time
$wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
# Check to see if the connection is done
if(!$wait)
{
# Close the connection and report timeout
$tcpclient.Close()
if($verbose){Write-Host "Connection Timeout"}
Return $false
}
else
{
# Close the connection and report the error if there is one
$error.Clear()
$tcpclient.EndConnect($iar) | out-Null
if(!$?){if($verbose){write-host $error[0]};$failed = $true}
$tcpclient.Close()
}
# Return $true if connection Establish else $False
if($failed){return $false}else{return $true}
Run Code Online (Sandbox Code Playgroud)
您可以转到该存储库页面进行后续操作(这个答案已经太多了复制工作)
小智 6
我见过的测试 TCP 端口的最短方法是:
(New-Object System.Net.Sockets.TcpClient).ConnectAsync("google.com", 80).Wait(100)
Run Code Online (Sandbox Code Playgroud)
或者
[System.Net.Sockets.TcpClient]::new().ConnectAsync("google.com", 80).Wait(100)
Run Code Online (Sandbox Code Playgroud)
等待时间以毫秒为单位。这是旧的 PowerShell 2.0 兼容方法。
采用@Jan 的答案,我让它不再那么混乱,现在它在生成的任务中对我有用。
抛出异常并且不依赖$error/API 用户使用非标准的“详细”内容是很好的(看起来.Connected很SocketException简洁)。
Function TestTCP { Param($address, $port, $timeout=2000)
$socket=New-Object System.Net.Sockets.TcpClient
try {
$result=$socket.BeginConnect($address, $port, $NULL, $NULL)
if (!$result.AsyncWaitHandle.WaitOne($timeout, $False)) {
throw [System.Exception]::new('Connection Timeout')
}
$socket.EndConnect($result) | Out-Null
$socket.Connected
}
finally {
$socket.Close()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36694 次 |
| 最近记录: |