Windows Netstat Command Tutorial with Examples To List Network Ports and Connections – POFTUT

Windows Netstat Command Tutorial with Examples To List Network Ports and Connections

Netstat is used to display active TCP connections and related listening ports in the computer or system. Actually, there are more features provided by netstat like display statistics about network stack protocols, IPv4, IPv6, TCP, UDP etc.

Syntax

Syntax of the netstat command is like below. Simply we can use the following options.

 

Display All TCP and UDP Connections with Listening Ports

TCP is the most used protocol for the transmission of packets between different hosts. In a regular usage for a host, there will be a lot of TCP connections in different phases. We can display all these connections with -a option like below.

Display All TCP Connections

Display All TCP Connections

We can see that while listing listening ports following information about these ports are provided.

  • Proto is the protocol the listening port is running. Generally, TCP and UDP are used.
  • Local Address is the local or current system IP address and ports number. The IP address and the port number is delimited with the :. 0.0.0.0 means all local IP addresses or network interfaces where 127.0.0.1 means only localhost or current system
  • Foreign Address is the remote IP address which is initiated a connection. Like Local address, IP address and the port number are delimited with the :.
  • State will provide the current status of the given port. A port can be listening which means accepting connections or CLOSED recently closed etc. More details about the port or TCP states can be found below.

TCP States

As we know TCP protocol provides reliable data transfer between hosts. TCP implements sessions to provide this reliability. From start to end there are different states in a TCP session. Here the sequence and meaning of TCP states.

  • LISTENING means the port is listening but do not have any connection with a remote host
  • ESTABLISHED the connection established and communicating with the remote host
  • TIME_WAIT the connection is in a wait situations
  • CLOSE_WAIT the connection is closing phase
  • CLOSED the connection is closed
  • SYN_RECEIVED the sync flag received to start the connection

Display Ethernet Statistics

Ethernet or MAC generally used for the same meaning. Ethernet is a layer 2 protocol used to communication in our LAN with other hosts and mostly with a gateway which is used to access other networks or internet. We can list detailed information about the ethernet protocol. We will use -e option to list ethernet statistics.

Display Ethernet Statistics

Display Ethernet Statistics

Following information about the Ethernet Statistics will be provided.

  • Received column is used to specify the received sizes
  • Sent column is used to specify the sent sizes
  • Bytes is used successfully completed transfers
  • Unicast packets generally related with the UDP protocol where there is no connection and sessions management
  • Non-unicast
  • Discards is the packets that are discarded because of the problems
  • Errors show the sizes of the packets where errors occurred
  • Unknown protocols show the protocols currently unknown by the TCP/IP stack

Display Numeric Presentation of Ports and Hostname

Host and ports generally have numeric and text presentations. netstat command by default try to resolve the host name and port name into text format. If we need to get the host and port numeric information like IP address and the port number we can use -n option.

Display Numeric Presentation of Ports and Hostname

Display Numeric Presentation of Ports and Hostname

Display Connection or Ports Process ID

All ports and connections are opened and managed by processes in the operating system. For example, Apache is a web server and uses TCP 80 for listening to HTTP requests. We can list processes id of given connection or port with -o option.

Display Connection or Ports Process ID

Display Connection or Ports Process ID

We can see that also PID or Process ID is provided which is the current application process ID which listens given port and interface.

Display Connection or Ports Process Name

Like the previous example, we can list established connection or listening port process name with -b option. But this option requires Administrator privileges.

Display Connection or Ports Process Name

Display Connection or Ports Process Name

We can see from the output that chrome.exe. established a connection with a remote host over https protocol.

Display Fully Qualified Domain Name

Normally netstat command list hostnames in a simple manner and with a fast way. It can skip some domain names too. We can for netstat to print fully qualified domain names with -f option.

Display Fully Qualified Domain Name

Display Fully Qualified Domain Name

We can see that only resolved DNS names or fully qualified domain names are shown like oracle.com

Display Only TCP Protocol

netstat command provide extensive filtering options according to protocols. We can provide filter option with  -p and protocol name. In this example, we will filter and show only TCP protocol.

Display Only TCP Protocol

Display Only TCP Protocol

As we can see there is no UDP protocol related port and connection information.

Display Only UDP Protocol

We can also filter and show only UDP protocol ports with -p udp option. Here we provided -a to list UDP too.

Display Only UDP Protocol

Display Only UDP Protocol

As we can see there is no TCP related port or connection information in this example.

Display Only IPv4

We can use -p ip option to filter and show only IPv4 connections. This can be useful generally because IPv6 protocol is not common.

Display Only IPv6

We can use -p ipv6 option to filter and show only IPv6 connections.

Display Statistics

netstat command provides a lot of statistical information about the network stack. These statistics provide detailed metrics about protocols. We can list this statistical information with -s option.

Display Statistics

Display Statistics

Display Only TCP Protocol Statistics

We can only list TCP protocol related statistics with -s -p tcp option.

Display Only TCP Protocol Statistics

Display Only TCP Protocol Statistics

As we can see from output there are the following information

  • Active Opens will list currently opened connection count. In this example, this is 104.
  • Passive Opens will list opened connections but not transferred any data recently. In this example, this is 15.
  • Failed Connection Attempts will list connection tries or attempts not completed so there are no started connections which are 4
  • Reset Connections will list connections ended with RST TCP flag.
  • Current Connectionswill list currently opened connection count which is 5 in this example.
  • Segments Receivedwill list count of received TCP segments.
  • Segments Sentwill list count of sent TCP segments.
  • Segments Retransmittedwill list the count of TCP segments of retransmitted.

Display Only ICMP Protocol Statistics

We can list only ICMP related statistics with -s -p icmp option.

Display Only ICMP Protocol Statistics

Display Only ICMP Protocol Statistics

Display Routing Table

Routing is used to set IP packets first hop according to their destination. Our system route information can be listed with -r option.

Display Routing Table

Display Routing Table

As we can see the default route is printed in the first line which IP address is 192.168.122.1 .

Display Interactively

If we need to list given options output interactively to monitor the metrics we can use interactive mode. Interactive mode is enabled by providing interval value to print output. This feature does not needs any option we will only provide interval value which is 2 in this case.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)