
Terminal - Commands tagged netstat - 37 results
netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ips[ip], ip | "sort -k1 -nr"}'
This is sample output - yours may be different.
netstat -antu | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -n
This is sample output - yours may be different.
Output contains also garbage (text parts from netstat's output) but it's good enough for quick check who's overloading your server.
lsof -i -n | grep ESTABLISHED
This is sample output - yours may be different.
Fast and easy way to find all established tcp connections without using the netstat command.
This is sample output - yours may be different.
netstat -an | grep --color -i -E 'listen|listening'
This is sample output - yours may be different.
watch "ss -nat | awk '"'{print $1}'"' | sort | uniq -c"
This is sample output - yours may be different.
3 CLOSE-WAIT
1 CLOSING
1013 ESTAB
6 FIN-WAIT-1
29 FIN-WAIT-2
20 LISTEN
1 State
323 TIME-WAIT
Monitoring TCP connections number showing each state. It uses ss instead of netstat because it's much faster with high trafic.
You can fgrep specific ports by piping right before awk:
watch "ss -nat | fgrep :80 | awk '"'{print $1}'"' | sort | uniq -c"
netstat -tn | awk '($4 ~ /:22\s*/) && ($6 ~ /^EST/) {print substr($5, 0, index($5,":"))}'
This is sample output - yours may be different.
netstat -an | grep 80 | wc -l
This is sample output - yours may be different.
Count on a specific port (80) - FreeBSD friendly.
netstat -tn | grep :80 | awk '{print $5}'| grep -v ':80' | cut -f1 -d: |cut -f1,2,3 -d. | sort | uniq -c| sort -n
This is sample output - yours may be different.
1 192.168.1.5
5 192.168.1.2
8 192.168.1.52
15 192.168.1.3
19 192.168.1.30
cut -f1,2 - IP range 16
cut -f1,2,3 - IP range 24
cut -f1,2,3,4 - IP range 24
netstat -Aan | grep .80 | grep -v 127.0.0.1 | grep EST | awk '{print $6}' | cut -d "." -f1,2,3,4 | sort | uniq
This is sample output - yours may be different.
Usually a nice list of IP addresses :-)
See who is using a specific port. Especially when you're using AIX. In Ubuntu, for example, this can easily be seen with the netstat command.
This is sample output - yours may be different.
Proto Recv-Q Send-Q Adresse locale Adresse distante Etat PID/Program name
tcp 0 0 0.0.0.0:8118 0.0.0.0:* LISTEN 1284/privoxy
tcp 0 0 0.0.0.0:4001 0.0.0.0:* LISTEN 1901/java
tcp 0 0 0.0.0.0:7175 0.0.0.0:* LISTEN 1546/postgres
udp 0 0 0.0.0.0:68 0.0.0.0:* 2028/dhclient
udp 0 0 0.0.0.0:5353 0.0.0.0:* 1394/avahi-daemon:
udp 0 0 0.0.0.0:46876 0.0.0.0:* 1394/avahi-daemon:
udp6 0 0 :::58041 :::* 1394/avahi-daemon:
udp6 0 0 :::5353 :::* 1394/avahi-daemon:
Check open TCP and UDP ports
This is sample output - yours may be different.
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 13443/apache2
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 801/sshd
tcp6 0 0 :::25565 :::* LISTEN 1249/java
tcp6 0 0 :::6667 :::* LISTEN 12101/bitlbee
tcp6 0 0 :::22 :::* LISTEN 801/sshd
While `lsof` will work, why not use the tool designed explicitly for this job?
(If not run as root, you will only see the names of PID you own)
sudo netstat|head -n2|tail -n1 && sudo netstat -a|grep udp && echo && sudo netstat|head -n2|tail -n1 && sudo netstat -a|grep tcp
This is sample output - yours may be different.
netstat -nt | awk -F":" '{print $2}' | sort | uniq -c
This is sample output - yours may be different.
count connections, group by IP and port
netstat -ntu | awk ' $5 ~ /^[0-9]/ {print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
This is sample output - yours may be different.
netstat has two lines of headers:
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
Added a filter in the awk command to remove them
netstat -ntu | awk ' $5 ~ /^[0-9]/ {print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
This is sample output - yours may be different.
watch 'netstat -anptu |egrep "^Proto|:80 "'
This is sample output - yours may be different.
Every 2.0s: netstat -anptu |egrep "^Proto|:80 " Wed May 18 11:03:25 2011
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18418/apache
tcp 0 0 64.xxx.183.xxx:80 213.xxx.243.xxx:21370 SYN_RECV -
tcp 0 0 64.xxx.183.xxx:80 213.xxx.243.xxx:6465 SYN_RECV -
tcp 0 0 64.xxx.202.xxx:80 213.xxx.243.xxx:30703 SYN_RECV -
tcp 0 0 64.xxx.202.xxx:80 64.xxx.202.xxx:39600 TIME_WAIT -
tcp 0 0 64.xxx.202.xxx:80 64.xxx.202.xxx:39601 TIME_WAIT -
Shows updated status in a terminal window for connections to port '80' in a human-friendly form. Use 'watch -n1' to update every second, and 'watch -d' to highlight changes between updates.
If you wish for status updates on a port other than '80', always remember to put a space afterwards so that ":80" will not match ":8080".
while sleep 1; do date; (netstat -a -n | grep 80) ; done
This is sample output - yours may be different.
netstat -rn | awk '/UG/{print $2}'
This is sample output - yours may be different.
Tested on CentOS, Ubuntu, and MacOS.
netstat -rn | grep UG | tr -s " " | cut -d" " -f2
This is sample output - yours may be different.
netstat -l -p --tcp | egrep -e 'www.*[0-9]{3,4}\/(apache2|httpd)' | awk '{print$7}'
This is sample output - yours may be different.
This is sample output - yours may be different.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nmbd 1381 root 10u IPv4 37683465 0t0 UDP *:137
nmbd 1381 root 13u IPv4 37683466 0t0 UDP *:138
nmbd 1381 root 14u IPv4 37683468 0t0 UDP 192.168.1.66:137
nmbd 1381 root 15u IPv4 37683469 0t0 UDP 192.168.1.66:138
nmbd 1381 root 16u IPv4 37683470 0t0 UDP 192.168.1.6:137
nmbd 1381 root 17u IPv4 37683471 0t0 UDP 192.168.1.6:138
smbd 1384 root 25u IPv6 37683528 0t0 TCP *:445 (LISTEN)
smbd 1384 root 26u IPv6 37683530 0t0 TCP *:139 (LISTEN)
smbd 1384 root 27u IPv4 37683532 0t0 TCP *:445 (LISTEN)
smbd 1384 root 28u IPv4 37683534 0t0 TCP *:139 (LISTEN)
portmap 1642 daemon 4u IPv4 4146 0t0 UDP *:111
portmap 1642 daemon 5u IPv4 4161 0t0 TCP *:111 (LISTEN)
rpc.statd 1658 statd 4u IPv4 4186 0t0 UDP *:986
rpc.statd 1658 statd 6u IPv4 4195 0t0 UDP *:50215
rpc.statd 1658 statd 7u IPv4 4198 0t0 TCP *:37962 (LISTEN)
cupsd 1689 root 5u IPv4 51957733 0t0 TCP *:631 (LISTEN)
cupsd 1689 root 16u IPv6 51957734 0t0 TCP *:631 (LISTEN)
cupsd 1689 root 18u IPv4 51957737 0t0 UDP *:631
dhclient 2031 root 5u IPv4 5307 0t0 UDP *:68
apache2 2093 root 4u IPv6 5480 0t0 TCP *:80 (LISTEN)
avahi-dae 2281 avahi 13u IPv4 5670 0t0 UDP *:5353
avahi-dae 2281 avahi 14u IPv6 5671 0t0 UDP *:5353
avahi-dae 2281 avahi 15u IPv4 5672 0t0 UDP *:60574
avahi-dae 2281 avahi 16u IPv6 5673 0t0 UDP *:37378
hddtemp 2705 root 0u IPv4 6851 0t0 TCP 127.0.0.1:7634 (LISTEN)
dhclient 3115 root 5u IPv4 10765 0t0 UDP *:68
exim4 13674 Debian-exim 4u IPv4 45844908 0t0 TCP 127.0.0.1:25 (LISTEN)
sshd 26220 root 3u IPv4 16151998 0t0 TCP *:22 (LISTEN)
sshd 26220 root 4u IPv6 16152000 0t0 TCP *:22 (LISTEN)
apache2 30313 www-data 4u IPv6 5480 0t0 TCP *:80 (LISTEN)
apache2 30316 www-data 4u IPv6 5480 0t0 TCP *:80 (LISTEN)
This command is more portable than it's cousin netstat. It works well on all the BSDs, GNU/Linux, AIX and Mac OS X. You won't find lsof by default on Solaris or HPUX by default, but packages exist around the web for installation, if needed, and the command works as shown. This is the most portable command I can find that lists listening ports and their associated pid.
This is sample output - yours may be different.
Shows you all listening tcp/udp ports, and what program has them open(depending on rights)
netstat -an | awk '/tcp/ {print $6}' | sort | uniq -c
This is sample output - yours may be different.
9 ESTABLISHED
9 LISTEN
1 SYN_SENT
Counts TCP states from Netstat and displays in an ordered list.
This is sample output - yours may be different.