首页 > 其他 > 详细

批量测试服务器网络端口脚本

时间:2019-04-17 23:54:57      阅读:230      评论:0      收藏:0      [点我收藏+]
#PS C:\Users\admin.MYAD\Desktop> .\test.ps1 -Dir C:\Users\admin.MYAD\Desktop\test.txt -Port 443

param($Dir,$Port)

function Test-PortAlive {
[CmdletBinding()]
[OutputType([System.boolean])]
param(
[Parameter(ValueFromPipeline=$true)][System.String[]]$server,
[int]$port
)

$socket = new-object Net.Sockets.TcpClient
$connect = $socket.BeginConnect($server, $port, $null, $null)
$NoTimeOut = $connect.AsyncWaitHandle.WaitOne(500, $false)

if ($NoTimeOut) {
    $socket.EndConnect($connect) | Out-Null
    return $true               
}
else {
    return $false
}

}

$DesktopPath=[Environment]::GetFolderPath("Desktop")+"\"
#echo $DesktopPath

$today=Get-Date
$LogFile=$DesktopPath+($today.ToString(‘yyyy-MM-dd‘))+".txt"
#echo $LogFile

$TxtCon=Get-Content($Dir)

foreach($LineCon in $TxtCon)
{

if(Test-PortAlive -port $Port $LineCon)
{
echo ("TCP Connect "+$LineCon+":"+$Port+" successful") | Out-File -Append $LogFile
}
else
{
echo ("TCP Connect "+$LineCon+":"+$Port+" fail") | Out-File -Append $LogFile
}

}

批量测试服务器网络端口脚本

原文:https://blog.51cto.com/642364/2380446

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!