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
4
votes
4 answers

java write netstat in cmd

My goal is to print all the internet connections on my computer. When I type netstat on CMD, I get the internet connections list. I wanted to do the same in Java, automatically. My code: Runtime runtime = Runtime.getRuntime(); process =…
Miigueel
  • 143
  • 2
  • 13
4
votes
1 answer

Windows API to get netstat -s statistics

I'm searching a windows API that return the same statistics of netstat -s
user2219913
  • 582
  • 2
  • 5
  • 13
4
votes
1 answer

GetExtendedTcpTable donesn't return the same result as netstat -ano

I used this code to get list of Opened port in my PC and the application that use each port. string Port::GetListOfTcpPorts() { string ApplicationName = ""; string result = ""; string aux = ""; string RemotePort = ""; DWORD…
user2198530
4
votes
3 answers

Get open ports as an array

So, I'm using netstat -lt to get open ports. However, I'm not interested in certain values (like SSH or 22), so I want to be able to exclude them. I also want to get them as an array in bash. So far I have netstat -lt | sed -r 's/tcp[^:]+://g' | cut…
Andy Shulman
  • 1,895
  • 3
  • 23
  • 32
4
votes
1 answer

active connections of nginx keep growing

I have several nginx web servers as reverse proxy. I found out that the active connections(including reading writing and waiting , seen from http_stub_status module) in some of the servers(not all of them) are keep growing from 3000 to 5000,…
user1417377
  • 51
  • 1
  • 1
  • 4
3
votes
1 answer

parsing ss output with awk

I want to get a list of applications and their output ports for some given list of applications, from my C program. I'm thinking of using something like ss -natp | awk '/firefox/ { split($4,array,":"); printf "%d ", array[2]} END{print ""}'' which…
Lavanya
  • 2,848
  • 3
  • 18
  • 13
3
votes
1 answer

How to get output from ExecDos::exec

When I execute netstat -a |find /C $portNumber in command prompt I get the total number of lines where the given port number exists. I want to get that same count in any variable so that I can check whether the port is already occupied or free in…
user1234
  • 289
  • 6
  • 13
3
votes
2 answers

How to List Active Ports and Processes using them in Linux, C Code

I am trying to write a C Code to do the same Job as: netstat -vatp List all Remote/Local Addresses and Processes using them. But I dunno which files should I be reading? I tried looking into /proc/net/tcp and /proc/net/udp, but they don't have the…
killercode
  • 1,666
  • 5
  • 29
  • 42
3
votes
2 answers

Get used ports and states

How can I get used ports and their states on Linux? Basically, everything that netstat can do, but in C?
Doddy
  • 1,311
  • 1
  • 17
  • 31
3
votes
2 answers

Why is the Recv-Q value in netstat equal to socket backlog + 1?

When I execute netstat -tulnp, the output is as follows: Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.11:43043 …
Poison
  • 389
  • 2
  • 14
3
votes
1 answer

Socket not listed in lsof but is listed in netstat

When I do "lsof -nl | egrep "TCP|UDP" " in order to see socket states, I notice that the sockets that I expected to be listed are not at all, but when I do a "netstat --tcp", they are listed, but are in a CLOSE_WAIT state. What I know so far is…
Sandra E
  • 55
  • 6
3
votes
2 answers

get IP addresses a program wants to connect to

my goal is that my code (C#) outputs the IP addresses another program is trying to connect to. Example: my browser.exe wants to connect to a website at 12.34.567.89 so my code would output that IP. Note: my main trouble is to also list unsuccessful…
Bloom
  • 33
  • 3
3
votes
1 answer

Cloud9: Could not find an open port

I'm new to NodeJS and trying to set up an existing project (developed by someone else) in Cloud9 IDE (I'm using an older Cloud9 account; so not running on AWS). I've pulled the git and installed everything. This all seemed to go without problems. To…
Nick
  • 3,496
  • 7
  • 42
  • 96
3
votes
2 answers

Address already in use: JVM_Bind but no process is listed using port with netstat

I have a java process that make use of ServerSocket API (Java 8) and bind to the local port 12000. Process runs under a Windows service Wrapper (exe4j). I have a scheduled task that runs at 4am that stop the service and restart it after a couple…
sbrisson
  • 346
  • 1
  • 9
3
votes
2 answers

ASP.NET Web API listens on 0.0.0.0 instead 127.0.0.1

My ASP.NET Web API is bound to localhost: startOptions.Urls.Add("http://localhost:8080"); If I now call netstat -a I would have expected to see something like 127.0.0.1:8080 but it looks like my binding works on all IP addresses on the local…
Dunken
  • 8,481
  • 7
  • 54
  • 87