Skip to content

Instantly share code, notes, and snippets.

@devlights
Created February 1, 2024 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devlights/6503390e68be5dd444a860ac2bc13ab7 to your computer and use it in GitHub Desktop.
Save devlights/6503390e68be5dd444a860ac2bc13ab7 to your computer and use it in GitHub Desktop.
PowerShellでシンプルなポートスキャン (ローカルでの確認用)
# スキャンするターゲットのIPアドレスまたはホスト名
$target = "xxx.xxx.xxx.xxx"
# スキャンするポート範囲
$startPort = 20
$endPort = 25
# 指定したポート範囲でスキャンを実行
for ($port = $startPort; $port -le $endPort; $port++) {
$tcpClient = New-Object System.Net.Sockets.TcpClient
$asyncResult = $tcpClient.BeginConnect($target, $port, $null, $null)
$waitHandle = $asyncResult.AsyncWaitHandle
$timeout = 1000 # タイムアウト時間(ミリ秒)
if ($waitHandle.WaitOne($timeout, $false)) {
if ($tcpClient.Connected) {
$tcpClient.Close()
"$port open"
} else {
"$port closed"
}
} else {
"$port closed"
}
$tcpClient.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment