pastebin - collaborative debugging tool
rovema.kpaste.net RSS


list_active_local_tcp_ports1.ksh - list port numbers of local TCP connections
Posted by Anonymous on Sun 26th Feb 2023 21:39
raw | new post
view followups (newest first): list_active_local_tcp_ports1.ksh - list port numbers of local TCP connections by Anonymous
modification of post by Anonymous (view diff)

  1. #!/usr/bin/ksh93
  2.  
  3. #
  4. # list_active_local_tcp_ports1.ksh - list port numbers of local TCP connections
  5. #
  6. # Written by Roland Mainz <roland.mainz@nrubsig.org>
  7. #
  8.  
  9. #    
  10. # simple netstat -n parser
  11. #    
  12. function netstat_list_connections
  13. {
  14. # fixme: should be set -o nounset clean
  15.         nameref data=$1
  16.        
  17.         typeset -a data.connections
  18.  
  19.         typeset nsout="$(LC_ALL='POSIX' netstat -n)"
  20.  
  21.         while read i ; do
  22.                 leftover="${i/~(Elrx)
  23.                 (?: # non-capturing group
  24.                         #
  25.                         # regex group for tcpudp
  26.                         #
  27.                         (tcp|tcp6|udp)  # Proto
  28.                         [[:space:]]+
  29.                         ([[:digit:]]+)                  # Recv-Q
  30.                         [[:space:]]+
  31.                         ([[:digit:]]+)                  # Send-Q
  32.                         [[:space:]]+
  33.                         ([^[:space:]]+)                 # Local Address
  34.                         [[:space:]]+
  35.                         ([^[:space:]]+)                 # Foreign Address
  36.                         [[:space:]]+
  37.                         ([^[:space:]]+)                 # State
  38.                 |
  39.                         #
  40.                         # regex for unix
  41.                         #
  42.                         (unix)                          # Proto
  43.                         [[:space:]]+
  44.                         ([[:digit:]]+)                  # RefCnt
  45.                         [[:space:]]+
  46.                         (\[.+\])                        # Flags
  47.                         [[:space:]]+
  48.                         ([^[:space:]]+)                 # Type
  49.                         [[:space:]]+
  50.                         ([^[:space:]]*?)                # State
  51.                         [[:space:]]+
  52.                         ([[:digit:]]+)                  # I-Node
  53.                         (?:
  54.                                 # Path is optional
  55.                                 |
  56.                                 [[:space:]]+
  57.                                 ([^[:space:]]+)         # Path
  58.                         )
  59.                 )
  60.                         /X}"
  61.        
  62.                 # if the regex above did not match then .sh.match
  63.                 # remains untouched, so we might see data from the
  64.                 # previous round.
  65.                 # so we check the "leftover" var whether it just
  66.                 # contains the dummy value of "X" to indicate a
  67.                 # successful regex match
  68.                 if [[ "$leftover" == 'X' ]] ; then
  69.                         #print -v .sh.match
  70.                        
  71.                         if [[ "${.sh.match[1]}" != '' ]] ; then
  72.                                 data.connections+=(
  73.                                         typeset proto="${.sh.match[1]}"
  74.                                         typeset recv_q="${.sh.match[2]}"
  75.                                         typeset send_q="${.sh.match[3]}"
  76.                                         typeset local_address="${.sh.match[4]}"
  77.                                         typeset foreign_address="${.sh.match[5]}"
  78.                                         typeset state="${.sh.match[6]}"
  79.                                 )
  80.                         elif [[ "${.sh.match[7]}" != '' ]] ; then
  81.                                 data.connections+=(
  82.                                         typeset proto="${.sh.match[7]}"
  83.                                         typeset refcnt="${.sh.match[8]}"
  84.                                         typeset flags="${.sh.match[9]}"
  85.                                         typeset type="${.sh.match[10]}"
  86.                                         typeset state="${.sh.match[11]}"
  87.                                         typeset inode="${.sh.match[12]}"
  88.                                         typeset path="${.sh.match[13]}"
  89.                                 )
  90.                         fi
  91.                 else
  92.                         :
  93.                         #printf "leftover=%q\n" "${leftover}"
  94.                 fi
  95.         done <<<"$nsout"
  96.        
  97.         return 0
  98. }        
  99.  
  100. function list_active_local_tcp_connections
  101. {
  102.         nameref ar=$1
  103.         compound c
  104.         integer port
  105.  
  106.  
  107.         netstat_list_connections c || return 1
  108. #       print -v c
  109.        
  110.         [[ -v ar ]] || typeset -a ar
  111.  
  112.         for i in "${!c.connections[@]}" ; do
  113.                 nameref n=c.connections[$i]
  114.                
  115.                 # look for only for TCP connections which match
  116.                 # 127.0.*.* or IPv6 ::1
  117.                 if [[ "${n.proto}" == ~(El)tcp && \
  118.                         "${n.local_address}" == ~(Elr)(127\.0\..+|::1):[[:digit:]]+ ]] ; then
  119.  
  120.                         port="${n.local_address##*:}"
  121.                         #printf $"port = %d\n" port
  122.  
  123.                         (( ar[port]=1 ))
  124.                 fi
  125.         done
  126.  
  127.         return 0
  128. }
  129.  
  130. function main
  131. {
  132.         compound c=( integer -a ar )
  133.  
  134.         list_active_local_tcp_connections c.ar || return 1
  135.  
  136. #       print -v c
  137.         printf $"%d\n" "${!c.ar[@]}"
  138.        
  139.         return 0
  140. }
  141.  
  142. # main entry point
  143. main
  144. exit $?
  145. # EOF.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at