click to expand ...

 

 


A unix like netstat command for use in the CLI shell of an SVC code based system.

 

Simply copy/paste that into a SVC CLI Shell (putty) window:

netstat - function definition
netstat () 
{
    read -a arr <<< $( < /proc/net/tcp )
    echo ${arr[*]:0:4}
    i=12
    while (( $i < ${#arr[@]} )); do
        if [[ "${arr[ $i ]}" =~ [0-9]+: && ${#arr[$i+1]} == 13 ]]; then 
            [[ ${arr[ $i ]} =~ ^([0-9]+) ]]
            printf "%3i: "  ${BASH_REMATCH[1]}
            [[ ${arr[ $(( i+1 )) ] } =~ (..)(..)(..)(..):(....) ]]
            printf "%-21s " $((0x${BASH_REMATCH[4]})).$((0x${BASH_REMATCH[3]})).$((0x${BASH_REMATCH[2]})).$((0x${BASH_REMATCH[1]})):$((0x${BASH_REMATCH[5]}))
            [[ ${arr[ $(( i+2 )) ] } =~ (..)(..)(..)(..):(....) ]]
            printf "%-21s " $((0x${BASH_REMATCH[4]})).$((0x${BASH_REMATCH[3]})).$((0x${BASH_REMATCH[2]})).$((0x${BASH_REMATCH[1]})):$((0x${BASH_REMATCH[5]}))
            case ${arr[ $i+3 ]} in 
                01) state_str=ESTABLISHED;;
                02) state_str=SYN_SENT;;
                03) state_str=SYN_RECV;;
                04) state_str=FIN_WAIT1;;
                05) state_str=FIN_WAIT2;;
                06) state_str=TIME_WAIT;;
                07) state_str=CLOSE;;
                08) state_str=CLOSE_WAIT;;
                09) state_str=LAST_ACK;;
                0A) state_str=LISTEN;;
                0B) state_str=CLOSING;;
                *) state_str='?'
            esac    
            printf "%s\n"  $state_str
            (( i += 4 ))
        else 
            (( i++ ))
        fi
    done
}

Usage examples

netstat - usage example
# show all tcp sessions of the config node

netstat

sl local_address rem_address st
  0: 192.168.55.20:427     0.0.0.0:0             LISTEN
  1: 127.0.0.1:427         0.0.0.0:0             LISTEN
  2: 0.0.0.0:50100         0.0.0.0:0             LISTEN
  3: 0.0.0.0:22            0.0.0.0:0             LISTEN
  4: 10.10.112.71:3260     0.0.0.0:0             LISTEN
  5: 192.168.55.20:22      10.10.120.73:64702    ESTABLISHED
  6: 10.10.112.71:3260     10.10.112.84:50863    ESTABLISHED
  7: 192.168.55.20:22      10.10.101.158:57083   ESTABLISHED
  8: 10.10.112.71:3260     10.10.112.82:62635    ESTABLISHED
  9: 192.168.55.20:22      10.10.206.89:55209    ESTABLISHED
 10: 10.10.112.71:3260     10.10.112.83:52089    ESTABLISHED
 11: 192.168.55.20:22      10.10.120.72:52433    ESTABLISHED
 12: 192.168.55.20:22      10.10.120.71:57889    ESTABLISHED
 13: 127.0.0.1:50100       127.0.0.1:58459       ESTABLISHED
 14: 192.168.55.20:22      10.10.120.48:62312    ESTABLISHED



 


 

Page viewed 3040 times by 7 users since Feb 13, 2013