- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-05-16T23:26:57+09:00","","")
#author("2018-05-16T23:29:06+09:00","","")
#navi(../)
* PowerShellで指定したポートのTCPが開放されているか調べる方法・Test-NetConnection [#s70014b0]
サーバに対して、ポートが開いているどうかの確認のため、Windowsであればtelnetコマンドをインストールして~
telnetコマンドにポート番号とサーバを指定しポートが開放されており、サービスが動作しているかどうかを確認することができました。~
PowerShellでは、Test-NetConnectionコマンドレットを使用することにより簡単に指定ポートが開放されているかどうか確認することができます。~
以下にいくつかの実行例を記します。
#contents
#htmlinsertpcsp(win-top.html,win-sp.html)
* 関連記事 [#cfd4bc92]
-[[PowerShellでpingをする方法>PowerShell/pingができるコマンドレットの紹介・Test-Connection]]
-[[指定したポートのTCPが開放されているか調べる方法・Test-NetConnection>PowerShell/指定したポートのTCPが開放されているか調べる方法・Test-NetConnection]]
* man Test-NetConnection [#j20645ee]
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 を使用してください。
* ポート番号を指定して確認 [#ued137e9]
以下のようにポート番号を指定してHTTPの80ポートが開放されているかを確認します。~
オプション-Portでポート番号を指定します。
Test-NetConnection -ComputerName win.just4fun.biz -Port 80
#ref(01.png)
#br
#bre(02.png)
#ref(02.png)
&color(red){上記のスクリーンショットをみると、Waiting for responseのメッセージが出力されています。};~
&color(red){これを''非表示''にする場合は、以下のようにします。};
* Waiting for response メッセージを非表示にする [#d14e7909]
非表示にするには、$ProgressPreferenceに''SilentlyContinue''を設定します。
変更前は以下のように Continue が設定されています。
PS C:\> $ProgressPreference
Continue
非表示にするには、以下のように変更します。
PS C:\> $ProgressPreference = "SilentlyContinue"
この設定後、Test-NetConnectionコマンドを実施してください。~
Waitng for responseのメッセージは表示されなくなります。
* プロトコルを指定して確認 [#lec5ad05]
オプションの-CommonTCPPortにプロトコル名を指定して実行してみます。
** HTTPプロトコルと指定した場合 [#ofd0b36f]
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プロトコルと指定した場合 [#ce9ef4df]
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
* エラーの場合 [#n28bfa0c]
ポート開放されていない場合の出力になります。~
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のみ返却してほしい場合 [#cda81382]
ポート開放されているかどうかの戻り値として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コマンドレットを使用してポートが開放されているかどうかを確認する方法でした。
#htmlinsertpcsp(win-btm.html,win-sp.html)