I'm trying to make a script (using Perl, but it isn't necessary) that will only count the number of Established, Time_Wait, and Closed_Wait connections on a system and print them in terminal. So far I've figured out that I can use :netstat -ant | awk '{print $6}' | sort | uniq -c | sort -nin order to print all of the connections, but when I run this from a script it will not print in terminal and it also gives me some connections that I am not looking for such as Listen and Foreign. The reason why it must only show Established, Time_Wait, and Closed_Wait is because the script is being used by a monitoring program that will fail if any other connection types show up. Can anyone make a suggestion? Thanks!
Script that only counts Established, TIME_WAIT, and Closed Wait connections using Netstat
linux;awk;scripting;perl;ksh
Your script can be slightly modified to only process the states you need:netstat -ant | awk '/ESTABLISHED|LISTEN|CLOSE_WAIT/ {print $6}' | \    sort | uniq -c | sort -nA further step would be to everything with awk, e.g. :netstat -ant | awk '/ESTABLISHED|LISTEN|CLOSE_WAIT/ {count[$6]++}END {  for(s in count) {    printf(%12s : %6d\n, s, count[s]);  }}'