#! /bin/sh
#
# summarise network connection on Solaris
# discard ephemeral ports
#
# Sample output:
# PASSIVE:
# Protocol No of Connections
# -------- -----------------
# ssh 1 = ESTABLISHED(1)
# sge_qmaster 17 = ESTABLISHED(17)
#
# ACTIVE:
# Protocol No of Connections
# -------- -----------------
# sge_qmaster 1 = ESTABLISHED(1)
# 80 1 = TIME_WAIT(1)
#
# shell function
# create the awk's BEGIN section, mapping service number to service name
serviceN2S()
{
awk '
BEGIN {
printf("BEGIN{")
}
/^[a-zA-Z]/ && $2 ~ /tcp$/ {
split($2,a,"/")
printf("service[\"%d\"]=\"%s\";", a[1], $1)
}
END {
printf("}")
}' /etc/services
}
b=`serviceN2S`
# ignore high port
ephemeral=`/usr/sbin/ndd /dev/tcp tcp_smallest_anon_port`
netstat -n -P tcp | nawk -v ephemeral=$ephemeral ''$b'
# awk function
# summarise the states of all connections
function details(List, arr1, arr2, i, sum, str) {
split(List, arr1, " ")
sum = 0
for ( i in arr1 ) {
if ( arr1[i] != "" ) {
++arr2[arr1[i]]
++sum
}
}
str = sprintf("%d = ", sum)
for ( i in arr2 ) {
str = sprintf("%s %s(%s)", str, i, arr2[i])
}
return str
}
/^[1-9][0-9]*\./ {
# passive
split($1, arr_p, ".")
if( int(arr_p[5]) < ephemeral ) {
passive[arr_p[5]] = sprintf("%s %s", passive[arr_p[5]], $NF)
}
# active
split($2, arr_a, ".")
if( int(arr_a[5]) < ephemeral ) {
active[arr_a[5]] = sprintf("%s %s", active[arr_a[5]], $NF)
}
}
END {
print "PASSIVE:"
printf("%-18s %s\n","Protocol","No of Connections")
printf("%-18s %s\n","--------","-----------------")
for ( i in passive ) {
if ( int(i) > ephemeral ) continue
sname = service[i]
if ( sname == "" ) {
sname = i
}
printf("%-18s %s\n", sname, details(passive[i]))
}
print ""
print "ACTIVE:"
printf("%-18s %s\n","Protocol","No of Connections")
printf("%-18s %s\n","--------","-----------------")
for ( i in active ) {
if ( int(i) > ephemeral ) continue
sname = service[i]
if ( sname == "" ) {
sname = i
}
printf("%-18s %s\n", sname, details(active[i]))
}
}'