1 Star 0 Fork 52

raingive / kasini3000

forked from chuanjiao10 / kasini3000 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
tcp--ping-v5.ps1 4.42 KB
一键复制 编辑 原始数据 按行查看 历史
# 例子 tcp--ping www.baidu.com 80
param
(
$MyComputerName = 'www.baidu.com',
[uint16]$port = '80',
[switch]$Quiet = $false,
[string]$AdapterNameLinux = ''
)
#Write-Verbose "$MyComputerName $port $Quiet"
if ( ($MyComputerName -eq 'www.baidu.com') -and ($port -eq '80') )
{
Write-Host "命令格式: tcp--ping 域名 端口" -ForegroundColor Yellow
}
try
{
[array]$ip地址组 = [System.Net.Dns]::GetHostAddresses($MyComputerName) | Where-Object { $_.AddressFamily -eq 'InterNetwork' -or $_.AddressFamily -eq 'InterNetworkV6' }
}
catch
{
if ($Quiet -eq $true)
{
return $false
}
else
{
Write-Host -NoNewline '【'
Write-Host -NoNewline $mycomputerName -ForegroundColor Red
Write-Host -NoNewline "】DNS解析失败!`n"
}
}
finally
{
}
#问:这个脚本谁写的?有问题找谁技术支持?
#答:QQ群号=183173532
#名称=powershell交流群
#华之夏,脚之巅,有我ps1片天!
#专门教学win,linux通用的ps1脚本。不想学也可以,入群用红包求写脚本。
foreach ($单个ip in $ip地址组)
{
if ($单个ip.AddressFamily -eq 'InterNetwork')
{
if ($单个ip.IPAddressToString -eq '0.0.0.0')
{
Write-Error '错误:是否需要ping 127.0.0.1 ?'
continue
}
#必须为每次连接,new对象。
$tcp对象4 = New-Object System.Net.Sockets.TCPClient ([System.Net.Sockets.AddressFamily]::InterNetwork)
#$tcp对象4.ReceiveTimeout = 1000
#$tcp对象4.SendTimeout = 1000
#这个参数不灵。导致超时默认20秒。
$connect4 = $tcp对象4.BeginConnect($单个ip, $port, $null, $null)
$wait = $Connect4.AsyncWaitHandle.WaitOne(2000, $false)
if ($Quiet -eq $true)
{
if ($tcp对象4.Connected -eq $true)
{
$tcp对象4.EndConnect($connect4)
$tcp对象4.Close()
$tcp对象4.Dispose()
return $true
}
else
{
$tcp对象4.Close()
$tcp对象4.Dispose()
return $false
}
}
else
{
if ($tcp对象4.Connected -eq $true)
{
$tcp对象4.EndConnect($connect4)
Write-Host -NoNewline "【$单个ip】【"
Write-Host -NoNewline "$port" -ForegroundColor Green
Write-Host -NoNewline "】"
Write-Host -NoNewline " 通了`n" -ForegroundColor Green
}
else
{
Write-Host -NoNewline "【$单个ip】【"
Write-Host -NoNewline "$port" -ForegroundColor Red
Write-Host -NoNewline "】"
Write-Host -NoNewline " 不通`n" -ForegroundColor Red
}
}
$tcp对象4.Close()
$tcp对象4.Dispose()
}
elseif ($单个ip.AddressFamily -eq 'InterNetworkV6')
{
$tcp对象6 = New-Object System.Net.Sockets.TCPClient ([System.Net.Sockets.AddressFamily]::InterNetworkV6)
#$tcp对象6.ReceiveTimeout = 1000
#$tcp对象6.SendTimeout = 1000
#这个参数不灵。导致超时默认20秒。
if ($IsLinux -eq $True)
{
if ($AdapterNameLinux -eq '')
{
$tcp对象6.Close()
$tcp对象6.Dispose()
Write-Error "错误:`nlinux下使用ipv6,需要提供参数: '-AdapterNameLinux eth0'。返回码1`n建议搭配此脚本使用:fh返回网卡名_mac_ip_linux.ps1"
exit 1
}
else
{
$单个ip = [ipaddress]$($单个ip.IPAddressToString + '%' + $AdapterNameLinux)
Write-Verbose $单个ip
}
}
$connect6 = $tcp对象6.BeginConnect($单个ip, $port, $null, $null)
$wait = $Connect6.AsyncWaitHandle.WaitOne(2000, $false)
if ($Quiet -eq $true)
{
if ($tcp对象6.Connected -eq $true)
{
$tcp对象6.EndConnect($connect6)
$tcp对象6.Close()
$tcp对象6.Dispose()
return $true
}
else
{
$tcp对象6.Close()
$tcp对象6.Dispose()
return $false
}
}
else
{
if ($tcp对象6.Connected -eq $true)
{
$tcp对象6.EndConnect($connect6)
Write-Host -NoNewline "【$单个ip】【"
Write-Host -NoNewline "$port" -ForegroundColor Green
Write-Host -NoNewline "】"
Write-Host -NoNewline " 通了`n" -ForegroundColor Green
}
else
{
Write-Host -NoNewline "【$单个ip】【"
Write-Host -NoNewline "$port" -ForegroundColor Red
Write-Host -NoNewline "】"
Write-Host -NoNewline " 不通`n" -ForegroundColor Red
}
}
$tcp对象6.Close()
$tcp对象6.Dispose()
}
else
{
Write-Error '错误:ip地址不是v4,也不是v6。错误码111'
}
}
Write-Host -ForegroundColor Yellow "`n大网站现在全都学坏了:`n每个域名每次解析出至少2个ip,每隔几分钟就换2个ip。`n所以你的【端口测试程序】也应该升级了"
exit 0
PowerShell
1
https://gitee.com/raingive/kasini3000.git
git@gitee.com:raingive/kasini3000.git
raingive
kasini3000
kasini3000
master

搜索帮助