サーバに対して、ポートが開いているどうかの確認のため、Windowsであればtelnetコマンドをインストールして
telnetコマンドにポート番号とサーバを指定しポートが開放されており、サービスが動作しているかどうかを確認することができました。
PowerShellでは、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
上記のスクリーンショットをみると、Waiting for responseのメッセージが出力されています。
これを非表示にする場合は、以下のようにします。
非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。 変更前は以下のように Continue が設定されています。
PS C:\> $ProgressPreference Continue
非表示にするには、以下のように変更します。
PS C:\> $ProgressPreference = "SilentlyContinue"
この設定後、Test-NetConnectionコマンドを実施してください。
Waitng for responseのメッセージは表示されなくなります。
オプションの-CommonTCPPortにプロトコル名を指定して実行してみます。
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
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の返却のみでよい場合は、-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コマンドレットを使用してポートが開放されているかどうかを確認する方法でした。