Questions tagged [netstat]

netstat (network statistics) is a command-line tool that displays network connections (both incoming and outgoing), routing tables, and a number of network interface statistics. It is available on Unix, Unix-like, and Windows NT-based operating systems.

netstat

netstat (network statistics) is a command-line tool that displays network connections (both incoming and outgoing), routing tables, and a number of network interface statistics. It is available on Unix, Unix-like, and Windows NT-based operating systems. It is used for finding problems in the network and to determine the amount of traffic on the network as a performance measurement.

Parameters

Parameters used with this command must be prefixed with a hyphen (-) rather than a slash (/).

  • -a : Displays all active connections and the TCP and UDP ports on which the computer is listening.
  • -b : Displays the binary (executable) program's name involved in creating each connection or listening port. (Windows XP, 2003 Server and newer Windows operating systems (not Microsoft Windows 2000 or other non-Windows operating systems)) On Mac OS X when combined with -i, the total number of bytes of traffic will be reported.
  • -e : Displays ethernet statistics, such as the number of bytes and packets sent and received. This parameter can be combined with -s.
  • -f : Displays fully qualified domain names for foreign addresses (only available on Windows Vista and newer operating systems).
  • -g : Displays multicast group membership information for both IPv4 and IPv6 (may only be available on newer operating systems)
  • -i : Displays network interfaces and their statistics (not available under Windows)
  • -m : Displays the STREAMS statistics.
  • -n : Displays active TCP connections, however, addresses and port numbers are expressed numerically and no attempt is made to determine names.
  • -o : Displays active TCP connections and includes the process ID (PID) for each connection. You can find the application based on the PID on the Processes tab in Windows Task Manager. This parameter can be combined with -a, -n, and -p. This parameter is available on Microsoft Windows XP, 2003 Server (and Windows 2000 if a hotfix is applied).[2]
  • -p Windows and BSD: Protocol : Shows connections for the protocol specified by Protocol. In this case, the Protocol can be tcp, udp, tcpv6, or udpv6. If this parameter is used with -s to display statistics by protocol, Protocol can be tcp, udp, icmp, ip, tcpv6, udpv6, icmpv6, or ipv6.
  • -p Linux: Process : Show which processes are using which sockets (similar to -b under Windows) (you must be root to do this)
  • -P Solaris: Protocol : Shows connections for the protocol specified by Protocol. In this case, the Protocol can be ip, ipv6, icmp, icmpv6, igmp, udp, tcp, or rawip.
  • -r : Displays the contents of the IP routing table. (This is equivalent to the route print command under Windows.)
  • -s : Displays statistics by protocol. By default, statistics are shown for the TCP, UDP, ICMP, and IP protocols. If the IPv6 protocol for Windows XP is installed, statistics are shown for the TCP over IPv6, UDP over IPv6, ICMPv6, and IPv6 protocols. The -p parameter can be used to specify a set of protocols.
  • -t Linux: Displays only TCP connections.
  • -v : When used in conjunction with -b it will display the sequence of components involved in creating the connection or listening port for all executables.
  • Interval : Redisplays the selected information every Interval seconds. Press CTRL+C to stop the redisplay. If this parameter is omitted, netstat prints the selected information only once.
  • -h (unix) /? (windows): Displays help at the command prompt.

Statistics Provided

Netstat provides statistics for the following:

Protocol - The name of the protocol (TCP or UDP).

Local Address - The IP address of the local computer and the port number being used. The name of the local computer that corresponds to the IP address and the name of the port is shown unless the -n parameter is specified. If the port is not yet established, the port number is shown as an asterisk (*).

Foreign Address - The IP address and port number of the remote computer to which the socket is connected. The names that corresponds to the IP address and the port are shown unless the -n parameter is specified. If the port is not yet established, the port number is shown as an asterisk (*).

State - Indicates the state of a TCP connection. The possible states are as follows: CLOSE_WAIT, CLOSED, ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, LAST_ACK, LISTEN, SYN_RECEIVED, SYN_SEND, and TIME_WAIT. For more information about the states of a TCP connection, see RFC 793.

Examples

To display the statistics for only the TCP or UDP protocols, type one of the following commands:

netstat -sp tcp
netstat -sp udp

To display active TCP connections and the process IDs every 5 seconds, type the following command (On Microsoft Windows, works on XP and 2003 only, or Windows 2000 with hotfix):

netstat -o 5

Mac OS X version:

netstat -w 5

To display active TCP connections and the process IDs using numerical form, type the following command (On Microsoft Windows, works on XP and 2003 only, or Windows 2000 with hotfix):

netstat -no

To display all ports open by a process with id pid:

netstat -aop | grep "pid"

Platform specific remarks

On the Windows platform, netstat information can be retrieved by calling the GetTcpTable and GetUdpTable functions in the IP Helper API, or IPHLPAPI.DLL. Information returned includes local and remote IP addresses, local and remote ports, and (for GetTcpTable) TCP status codes. In addition to the command-line netstat.exe tool that ships with Windows, GUI-based netstat programs are available.

On the Windows platform, this command is available only if the Internet Protocol (TCP/IP) protocol is installed as a component in the properties of a network adapter in Network Connections.

On Mac OS X 10.5, the above option "-o" is not available. With Mac OS X 10.5, the /Applications/Utilities folder contains a network utility called: Network Utility, see tab Netstat for these stats presented in a gui application, along with Ping, Lookup, Traceroute, Whois, Finger and Port Scan.

Wikipedia Entry for netstat

548 questions
3
votes
1 answer

Is a FIN_WAIT2 state ever due to a close-connection initiator?

I'm doing a bit of POSIX socket programming and am running into a problem. I've wrote an application that uses nonblocking sockets. Because I'm currently developing a client against a server that is in develop, bad application level messages…
Izzo
  • 4,461
  • 13
  • 45
  • 82
3
votes
0 answers

Send-Q column in netstat vs ss output

I am using netstat to retrieve the maximum size of the syn backlog from each listening socket. I read the following from the documentation: Send-Q Established: The count of bytes not acknowledged by the remote host. Listening: Since Kernel…
bchetioui
  • 326
  • 1
  • 6
3
votes
1 answer

Why isn't my port exposed? netstat output included

This is my problem # docker exec -ti root_web_1 bash [root@ca32f79bdc14]# curl couchdb:5984 curl: (7) Failed to connect to couchdb port 5984: Connection refused [root@ca32f79bdc14]# curl redis:6379 -ERR wrong number of arguments for 'get'…
Jasmine Lognnes
  • 6,597
  • 9
  • 38
  • 58
3
votes
1 answer

Why is my socket's open port not listed by netstat?

If you run this example, you'll see the port is never listed by netstat. Why? And how do I make it so? #include #include #include #pragma comment(lib, "WS2_32") int main() { WORD wVers = MAKEWORD(2, 2); WSADATA…
user541686
  • 205,094
  • 128
  • 528
  • 886
3
votes
1 answer

Don't understand simple perl script in my crontab

First of all, sorry for my english, also is my first question (Dont know what I'm doing :)). I trying to edit my cron job and listing 'my' scheduled tasks i found this one: */5 * * * * perl /usr/bin/hm_fix.pl > /dev/null 2<&1 The script…
antonrodin
  • 56
  • 4
3
votes
1 answer

How to do like "netstat -p", but faster?

Both "netstat -p" and "lsof -n -i -P" seems to readlinking all processes fd's, like stat /proc/*/fd/*. How to do it more efficiently? My program wants to know what process is connecting to it. Traversing all processes again and again seems too…
Vi.
  • 37,014
  • 18
  • 93
  • 148
3
votes
2 answers

Find if a port is available to use in linux using c++

I am working on a C++ Project. To fulfill one of the requirement, I need to check if a port is available for using in my application anytime. To fulfill this , I have come to this following solution. #include #include #include…
Surajeet Bharati
  • 1,363
  • 1
  • 18
  • 36
3
votes
3 answers

`netstat` doesn't show sockets binded by python SimpleHTTPServer?

A local server is run using the SimpleHTTPServer module from Python 2.7 $ python -m SimpleHTTPServer Serving HTTP on 0.0.0.0 port 8000 ... Then I use netstat to search for that socket using 8000 as a filter for port number, however, I can't find…
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
3
votes
1 answer

UNIX expression to filter out PID from netstat output

Below is Output of command: netstat -plten|grep 45678 tcp 0 0 :::45678 :::* LISTEN 0 129367622 109746/java However, I want to get ‘109746’ as output. I tried: netstat…
muhdchoaib
  • 33
  • 5
3
votes
1 answer

How find PID by port in windows and kill found tasks using java

I need kill process in java code by process port. I can do it manually in cmd like: C:\>netstat -a -n -o | findstr :6543 TCP 0.0.0.0:6543 0.0.0.0:0 LISTENING 1145 TCP [::]:6543 [::]:0 …
khris
  • 4,809
  • 21
  • 64
  • 94
3
votes
2 answers

Find application using port

I have a networking program setup that does a lot of what the nestat program does. I am working now on netstat -o. That command will give me the PID of the program using that socket. I have some idea of where to look. I have been trying to use…
Paul
  • 137
  • 3
  • 12
3
votes
1 answer

netstat bat script recording all RDP connections

The aim of this bat file is to record RDP connections and log them to a file when the user logs on, it works ok but it records all RDP connections, so it is not accurate... is it possible just to run the netstat cmd for a single user at logon? i.e.…
jbcom41
  • 49
  • 1
  • 1
  • 3
3
votes
2 answers

How to kill a CLOSE_WAIT state process preferably by port 80 in WINDOWS with batch script

Summary: Rogue java processes from lingering stopped services preventing service from returning. Stopping a java service doesn't kill the java process on occasion, it locks it in a CLOSE_WAIT status endlessly, so port 80 is still being used on the…
3
votes
2 answers

Netstat focus on (find port)

Im recently trying to execute the following line ; string strCmdText; strCmdText = "netstat -np TCP | find " + quote + number + quote + ""; System.Diagnostics.Process.Start("netstat.exe", strCmdText); Logs.Write("LISTEN_TO(" + Registry_val1.Text +…
Cotemp
  • 55
  • 1
  • 4
3
votes
3 answers

How do I determine if a port is in use, e.g. via netstat?

I am trying to check port availability and get a return value using shell script. Example: if port 8080 is free then return true, else return false. Can anyone help? I tried with netstat.
max
  • 613
  • 3
  • 13
  • 26