Last active
November 7, 2017 13:48
-
-
Save ooltcloud/5a8e96e92ec82cbd81f2 to your computer and use it in GitHub Desktop.
TCP のサービスにアクセスできるかを確認する (TCPing もどき)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ipv4,ipv6 共通処理 | |
Function executeTCPing($tcp) { | |
$sw = New-Object System.Diagnostics.Stopwatch | |
$sw.Start() | |
try { | |
$tcp.Connect($target, $port) | |
$sw.Stop() | |
Write-Output ("{0} port={1} ({2}) への接続: 時間 ={3:0.00}ms" -f $target, $port, $tcp.Client.RemoteEndPoint, ($sw.Elapsed).TotalMilliseconds) | |
} catch { | |
$sw.Stop() | |
Write-Output ("{0} port={1} への接続: 応答が確認できません" -f $target, $port, ($sw.Elapsed).TotalMilliseconds) | |
} finally { | |
$tcp.Close() | |
$tcp.Dispose() # エラーが出る場合は無くても良い | |
} | |
} | |
# ipv4 用 | |
Function TCPing($target, $port) { | |
$tcp = New-Object System.Net.Sockets.TcpClient | |
executeTCPing $tcp | |
} | |
# ipv6 用 | |
Function TCPing6($target, $port) { | |
$ipv6 = [System.Net.Sockets.AddressFamily]::InterNetworkV6 | |
$tcp = New-Object System.Net.Sockets.TcpClient $ipv6 | |
executeTCPing $tcp | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
使い方 | |
PS > TCPing localhost 445 | |
localhost port=445 (127.0.0.1:445) への接続: 時間 =0.81ms | |
PS > TCPing localhost 80 | |
localhost port=80 への接続: 応答が確認できません |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment