PowerShell实现测试端口可用性脚本分享
利用简单的TCP套接字来简单判断一个端口是否可用:
代码如下:
Function Test-PortAvailable { param( [validaterange(1,65535)] [int]$Port ) $sockt=New-Object System.Net.Sockets.Socket -ArgumentList 'InterNetwork','Stream','TCP' $ip = (Get-NetIPConfiguration).IPv4Address | Select -First 1 -ExpandProperty IPAddress $ipAddress = [Net.IPAddress]::Parse($ip) Try { $ipEndpoint = New-Object System.Net.IPEndPoint $ipAddress,$port $sockt.Bind($ipEndpoint) return $true } Catch [exception] { return $false } Finally { $sockt.Close() } }
使用示例:
代码如下:
PS> Test-PortAvailable -Port 102 True PS> Test-PortAvailable -Port 1025 False
相关推荐
咻咻ing 2020-05-28
lucialee 2020-03-03
80443361 2020-02-28
jszy 2020-02-24
mohanzb 2019-12-05
today0 2016-10-28
Testingba工作室 2016-03-17
灰蓝 2017-07-19
sharpljc 2011-12-20
phpboy 2011-12-19
phpboy 2011-08-12
yelinag 2016-03-16
liuuil0 2015-02-04
gilbertzhyin 2010-11-16
thug 2019-06-20
YBsisterchang 2012-01-06
xiaoyezihanghui 2019-05-23
花江 2017-12-07