このエントリーをはてなブックマークに追加


PowerShellで指定したポートのTCPが開放されているか調べる方法・Test-NetConnection

サーバに対して、ポートが開いているどうかの確認のため、Windowsであればtelnetコマンドをインストールして
telnetコマンドにポート番号とサーバを指定しポートが開放されており、サービスが動作しているかどうかを確認することができました。
PowerShellでは、Test-NetConnectionコマンドレットを使用することにより簡単に指定ポートが開放されているかどうか確認することができます。
以下にいくつかの実行例を記します。


関連記事

man Test-NetConnection

Test-NetConnectionコマンドレットの使用方法です。

PS C:\> man Test-NetConnection

名前
    Test-NetConnection

構文
    Test-NetConnection [[-ComputerName] <string>]  [<CommonParameters>]

    Test-NetConnection [[-ComputerName] <string>] [-CommonTCPPort] {HTTP | RDP | SMB | WINRM}  [<CommonParameters>]

    Test-NetConnection [[-ComputerName] <string>]  [<CommonParameters>]

    Test-NetConnection [[-ComputerName] <string>]  [<CommonParameters>]


エイリアス
    TNC


注釈
    Get-Help を実行しましたが、このコンピューターにこのコマンドレットのヘルプ ファイルは見つかりませんでした。ヘルプの一部だけが表示されています。
        -- このコマンドレットを含むモジュールのヘルプ ファイルをダウンロードしてインストールするには、Update-Help を使用してください。

ポート番号を指定して確認

以下のようにポート番号を指定してHTTPの80ポートが開放されているかを確認します。
オプション-Portでポート番号を指定します。

Test-NetConnection -ComputerName win.just4fun.biz -Port 80
01.png
 
02.png

上記のスクリーンショットをみると、Waiting for responseのメッセージが出力されています。
これを非表示にする場合は、以下のようにします。

Waiting for response メッセージを非表示にする

非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。 変更前は以下のように Continue が設定されています。

PS C:\> $ProgressPreference
Continue

非表示にするには、以下のように変更します。

PS C:\> $ProgressPreference = "SilentlyContinue"

この設定後、Test-NetConnectionコマンドを実施してください。
Waitng for responseのメッセージは表示されなくなります。

プロトコルを指定して確認

オプションの-CommonTCPPortにプロトコル名を指定して実行してみます。

HTTPプロトコルと指定した場合

PS C:\> Test-NetConnection -ComputerName win.just4fun.biz -CommonTCPPort HTTP


ComputerName     : win.just4fun.biz
RemoteAddress    : 157.7.107.123
RemotePort       : 80
InterfaceAlias   : Wi-Fi
SourceAddress    : 192.168.1.24
TcpTestSucceeded : True

SMBプロトコルと指定した場合

PS C:\> Test-NetConnection -ComputerName pi.local -CommonTCPPort SMB


ComputerName     : pi.local
RemoteAddress    : 192.168.1.81
RemotePort       : 445
InterfaceAlias   : Wi-Fi
SourceAddress    : 192.168.1.24
TcpTestSucceeded : True

エラーの場合

ポート開放されていない場合の出力になります。
TcpTestSucceededがFalseになっているのが確認できます。

  • サーバが存在しない
    PS C:\> Test-NetConnection -ComputerName foo -Port 123
    警告: Name resolution of foo failed
    
    
    ComputerName   : foo
    RemoteAddress  :
    InterfaceAlias :
    SourceAddress  :
    PingSucceeded  : False
  • ポート開放されていない場合
    PS C:\> $result = Test-NetConnection -ComputerName pi.local -Port 123
    警告: TCP connect to (192.168.1.81 : 123) failed
    PS C:\> $result
    
    
    ComputerName           : pi.local
    RemoteAddress          : 192.168.1.81
    RemotePort             : 123
    InterfaceAlias         : Wi-Fi
    SourceAddress          : 192.168.1.24
    PingSucceeded          : True
    PingReplyDetails (RTT) : 1 ms
    TcpTestSucceeded       : False
    
    
    
    PS C:\> $result.TcpTestSucceeded
    False

True/Falseのみ返却してほしい場合

ポート開放されているかどうかの戻り値としてTrueまたはFalseの返却のみでよい場合は、-InformationLevel Quietオプションを追記します。

PS C:\> Test-NetConnection -ComputerName pi.local -Port 22 -InformationLevel Quiet
True
PS C:\> Test-NetConnection -ComputerName pi.local -Port 23 -InformationLevel Quiet
警告: TCP connect to (192.168.1.81 : 23) failed
False

以上、PowerShellのTest-NetConnectionコマンドレットを使用してポートが開放されているかどうかを確認する方法でした。



添付ファイル: file02.png 5237件 [詳細] file01.png 1448件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-05-16 (水) 23:29:06