0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Test-Connectionを使ってTCPポートをスキャンする

Posted at

foreachループを使って、対象のIPアドレスの到達性とTCPポートの状態を確認する

ユースケース

  • foreachでテキストのリストと変数の配列から対象を取り出す
  • foreachの中でforeachを回すネスト構造
  • Test-Connection-TcpPortオプションでポートの状態を確認

以下、環境です。
Windows 10 Enterprise
PowerShe 7.2.5

※ Windows Powersell(5.1)では、-TcpPortのオプションは使えないので注意
※ 参考サイト:Microsoft : Test-Connection

ipAddrList.txt
172.22.1.201
172.22.1.170
172.22.1.179
172.22.1.81
172.22.1.26
172.22.1.25
172.22.0.1
172.22.0.2
  • IP到達性を確認したいIPアドレスリスト
tcpScanPort.ps1
$ipAddrs = (Get-Content ./ipAddrList.txt)
$scanPorts = @(22, 80, 443)

Start-Transcript C:\Temp\scanResult.txt

foreach ($ips in $ipAddrs) {
    $pingResult = Test-Connection $ips -Count 1 -Quiet
    IF ($pingResult -eq "True") {
        foreach ($port in $scanPorts) {
        $scanResult = Test-Connection $ips -IPv4 -TcpPort $port -TimeoutSeconds 1
            IF ($scanResult -eq "True") {
                Write-Host "Host:" $ips "Port:" $port "is open"
            } else {
                Write-Host "Host:" $ips "Port:" $port "is closed" 
            }
        }
    } else {
        Write-Host "Host:" $ips "is NOT reachable!"
    }
}

Stop-Transcript
  • 実行結果
PS C:\Users\MashCannu> .\tcpPortScan.ps1
Transcript started, output file is C:\Temp\scanResult.txt
Host: 172.22.1.201 Port: 22 is closed
Host: 172.22.1.201 Port: 80 is closed
Host: 172.22.1.201 Port: 443 is closed
Host: 172.22.1.179 is NOT reachable!
Host: 172.22.1.170 Port: 22 is closed
Host: 172.22.1.170 Port: 80 is closed
Host: 172.22.1.170 Port: 443 is closed
Host: 172.22.1.81 Port: 22 is closed
Host: 172.22.1.81 Port: 80 is closed
Host: 172.22.1.81 Port: 443 is closed
Host: 172.22.1.26 Port: 22 is closed
Host: 172.22.1.26 Port: 80 is open
Host: 172.22.1.26 Port: 443 is open
Host: 172.22.1.25 Port: 22 is closed
Host: 172.22.1.25 Port: 80 is open
Host: 172.22.1.25 Port: 443 is closed
Host: 172.22.0.1 Port: 22 is closed
Host: 172.22.0.1 Port: 80 is closed
Host: 172.22.0.1 Port: 443 is closed
Host: 172.22.0.2 is NOT reachable!
Transcript stopped, output file is C:\Temp\scanResult.txt
PS C:\Users\MashCannu>

コード解説

line1.ps1
$ipAddrs = (Get-Content ./ipAddrList.txt)
$scanPorts = @(22, 80, 443)
  • IPアドレスはテキストファイル、ポートは配列からとってくる
line2.ps1
Start-Transcript C:\Temp\scanResult.txt

... code ....

Stop-Transcript
  • コードの実行結果をStart/Stop-Transcriptでテキストファイルに保存
line3.ps1
$pingResult = Test-Connection $ips -Count 1 -Quiet
$scanResult = Test-Connection $ips -IPv4 -TcpPort $port -TimeoutSeconds 1
  • Test-Connectionの結果をBoolean型で取得し、それぞれの変数に入れる
    -Quiet-TcpPort のオプションを付けると返値はBoolean型になる。
line4.ps1
foreach ($ips in $ipAddrs) {
    $pingResult = Test-Connection $ips -Count 1 -Quiet
    IF ($pingResult -eq "True") {
        foreach ($port in $scanPorts) {
        $scanResult = Test-Connection $ips -IPv4 -TcpPort $port -TimeoutSeconds 1

    } else {
        
    }
  • 最初のforeachループで対象のIPアドレスの到達性を確認する
  • IF文で到達性を判断し、到達できた場合は次のforeachループを回す
  • 到達できない場合は、elseブロックないの記述を処理
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?