PowerShell – Network Command – “Test-NetConnection”

Background

Continuing with preparing for our planned Network work.

Lineage

  1. PowerShell – Network Command – “Get-NetAdapter”
    Link

Test-NetConnection

Outline

The Test-NetCommand provides basic network connection testing over the TCP Protocol.

Exercises

Exercise – Basic

Outline

  1. Basic network connectivity test
  2. TCP Network Port Test
    • Network Port Number specified as numeric
  3. “Common TCP” Port Port Test
  4. Diagnostic Routing
    • Any Network Interface
    • Over Specific Network Interface
  5. Trace Route

 

Syntax


powershell -Command "test-netconnection -ComputerName [hostname]"

Sample


powershell -Command "test-netconnection -ComputerName lab"

Output

Output-Image

basic.01.20200501.1026AM

Output-Text
>powershell -Command "test-netconnection -ComputerName lab"

ComputerName           : lab
RemoteAddress          : 192.168.0.11
InterfaceAlias         : Wi-Fi
SourceAddress          : 192.168.0.9
PingSucceeded          : True
PingReplyDetails (RTT) : 1 ms

Explanation

  1. ICMP traffic is allowed between my host and the targeted host
  2. I am going over my WIFI Interface card

Exercise – Port Connectivity

Outline

  1. Network Port network connectivity test
  2. Protocol TCP

Syntax


powershell -Command "test-netconnection -ComputerName [hostname]  -Port [portNumber] -InformationLevel Detailed "

Sample


powershell -Command "test-netconnection -ComputerName lab -Port 22  -InformationLevel Detailed "

Output

Output-Image

network.port.ssh.01.20200501.0149PM.PNG

Output-Text

ComputerName            : lab
RemoteAddress           : 192.168.70.92
RemotePort              : 22
NameResolutionResults   : 192.168.70.92
MatchingIPsecRules      :
NetworkIsolationContext : Internet
InterfaceAlias          : Wi-Fi
SourceAddress           : 192.160.70.11
NetRoute (NextHop)      : 0.0.0.0
TcpTestSucceeded        : True

Explanation

  1. SSH traffic is allowed between my host and the targeted host

Exercise – Common TCP Protocol Connectivity

Outline

  1. Network Port network connectivity test
  2. Protocol TCP
  3. Common TCP Protocols Supported
    • The Supported Protocols
      • SMB
      • HTTP
      • RDP
      • WINRM

Syntax


powershell -Command "test-netconnection -ComputerName [hostname]  -CommonTCPPort [protocol] -InformationLevel Detailed "

Sample

Sample 01

powershell -Command "test-netconnection -ComputerName lab -CommonTCPPort SMB  -InformationLevel Detailed "

Output
Output-Image

network.port.common.port.tcp.smb.01.20200501.0214PM

Output-Text
>powershell -Command "test-netconnection -ComputerName lab -CommonTCPPort SMB -InformationLevel Detailed "
WARNING: TCP connect to lab:445 failed

ComputerName            : lab
RemoteAddress           : 192.168.17.101
RemotePort              : 445
NameResolutionResults   : 192.168.17.101
MatchingIPsecRules      :
NetworkIsolationContext : Internet
InterfaceAlias          : Wi-Fi
SourceAddress           : 192.168.17.9
NetRoute (NextHop)      : 0.0.0.0
PingSucceeded           : True
PingReplyDetails (RTT)  : 1 ms
TcpTestSucceeded        : False

Explanation
  1. TCP Connection to host lab over port 445 denied
  2. Microsoft SMB uses TCP Port 445
Sample -02

powershell -Command "test-netconnection -ComputerName labFileServer -CommonTCPPort SMB -InformationLevel Detailed "

Output
Output-Image

network.port.common.port.tcp.smb.fileServer.02.20200501.0231PM

Output-Text
>powershell -Command "test-netconnection -ComputerName labFileServer -CommonTCPPort SMB -InformationLevel Detailed "

ComputerName            : labFileServer
RemoteAddress           : 192.168.10.92
RemotePort              : 445
NameResolutionResults   : 192.168.10.92
MatchingIPsecRules      :
NetworkIsolationContext : Internet
InterfaceAlias          : Wi-Fi
SourceAddress           : 192.168.10.4
NetRoute (NextHop)      : 0.0.0.0
TcpTestSucceeded        : True

Explanation
  1. TCP Connection to host labFileServer over port 445 succeeded
  2. Microsoft SMB uses TCP Port 445

Exercise – Common TCP Protocol Connectivity – Constrain Interface

Outline

  1. Network Port network connectivity test
  2. Protocol TCP
  3. The Key difference is that we are restricting our test to a specific Network Interface Card
    • Constrain Interface
      • The number we arrived at Get-NetAdapter
      • Here is the Link

Syntax


powershell -Command "test-netconnection -ComputerName [hostname] DiagnoseRouting -ConstrainInterface [interfaceNumber] -InformationLevel Detailed "

Sample

Sample 01

powershell -Command "test-netconnection -ComputerName \"www.bbc.com\" -DiagnoseRouting -ConstrainInterface 3"

Output
Output-Image

network.port.common.port.tcp.diagnoseRouting.01.20200501.0248PM

Output-Text

ComputerName              : www.bbc.com
RemoteAddress             : 151.101.188.81
ConstrainInterfaceIndex   : 3
SelectedSourceAddress     : 192.168.0.74
OutgoingInterfaceIndex    : 3
SelectedNetRoute          : DestinationPrefix: 0.0.0.0/0
                            NextHop: 192.168.0.1
RouteDiagnosticsSucceeded : True

Explanation
  1. Destination
  2. Source
    • SeletedSourceAddress:- 192.168.0.74
  3. Outgoing Interface Index
    • Requested Interface
  4. Selected Net Route
    • Default Route:- (0.0.0.0/0)
    • NextHop:-
      • In our case 192.168.0.1
      • In your case, it will be your router’s/gateway IP Address

Exercise – Trace Route

Outline

  1. Network Port network connectivity test
  2. Protocol TCP
  3. Trace Route
    • Show me the route that you will take to reach the endpoint

Syntax


powershell -Command "test-netconnection -ComputerName [hostname] TraceRoute -InformationLevel Detailed "

Sample

Sample 01

powershell -Command "test-netconnection -ComputerName \"www.bbc.com\" -] TraceRoute -InformationLevel Detailed "

Output
Output-Image

network.port.traceroute.01.20200505.0706PM

Output-Text

ComputerName           : www.bbc.com
RemoteAddress          : 151.101.40.81
NameResolutionResults  : 151.101.40.81
InterfaceAlias         : Wi-Fi
SourceAddress          : 192.168.0.102
NetRoute (NextHop)     : 192.168.0.70
PingSucceeded          : True
PingReplyDetails (RTT) : 19 ms
TraceRoute             : 96.120.88.189
                         68.87.197.153
                         162.151.86.221
                         162.151.79.9
                         68.86.91.73
                         68.86.84.250
                         96.110.32.246
                         173.167.57.86
                         151.101.40.81

Explanation
  1. Destination
  2. Source
    • Source Address:- 192.168.0.102
  3. Next Route
    • Next Route:- 192.168.0.70
  4. Route:-
    • 96.120.88.189
    • 68.87.197.153
    • 162.151.86.221
    • 162.151.79.9
    • 68.86.91.73
    • 68.86.84.250
    • 96.110.32.246
    • 173.167.57.86
    • 151.101.40.81

References

  1. Microsoft
    • Docs / Windows / Powershell
      • Module:- nettcpip
        • Test-NetConnection
          Link
      • Module:- netadapter
        • Get-NetAdapter
          Link
  2. Reddit
    • Test-NetConnection from specific NIC
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s