- #!/usr/bin/ksh93
- #
- # list_active_local_tcp_ports1.ksh - list port numbers of local TCP connections
- #
- # Written by Roland Mainz <roland.mainz@nrubsig.org>
- #
- #
- # simple netstat -n parser
- #
- function netstat_list_connections
- {
- # fixme: should be set -o nounset clean
- nameref data=$1
- typeset -a data.connections
- typeset nsout="$(LC_ALL='POSIX' netstat -n)"
- while read i ; do
- leftover="${i/~(Elrx)
- (?: # non-capturing group
- #
- # regex group for tcpudp
- #
- (tcp|tcp6|udp) # Proto
- [[:space:]]+
- ([[:digit:]]+) # Recv-Q
- [[:space:]]+
- ([[:digit:]]+) # Send-Q
- [[:space:]]+
- ([^[:space:]]+) # Local Address
- [[:space:]]+
- ([^[:space:]]+) # Foreign Address
- [[:space:]]+
- ([^[:space:]]+) # State
- |
- #
- # regex for unix
- #
- (unix) # Proto
- [[:space:]]+
- ([[:digit:]]+) # RefCnt
- [[:space:]]+
- (\[.+\]) # Flags
- [[:space:]]+
- ([^[:space:]]+) # Type
- [[:space:]]+
- ([^[:space:]]*?) # State
- [[:space:]]+
- ([[:digit:]]+) # I-Node
- (?:
- # Path is optional
- |
- [[:space:]]+
- ([^[:space:]]+) # Path
- )
- )
- /X}"
- # if the regex above did not match then .sh.match
- # remains untouched, so we might see data from the
- # previous round.
- # so we check the "leftover" var whether it just
- # contains the dummy value of "X" to indicate a
- # successful regex match
- if [[ "$leftover" == 'X' ]] ; then
- #print -v .sh.match
- if [[ "${.sh.match[1]}" != '' ]] ; then
- data.connections+=(
- typeset proto="${.sh.match[1]}"
- typeset recv_q="${.sh.match[2]}"
- typeset send_q="${.sh.match[3]}"
- typeset local_address="${.sh.match[4]}"
- typeset foreign_address="${.sh.match[5]}"
- typeset state="${.sh.match[6]}"
- )
- elif [[ "${.sh.match[7]}" != '' ]] ; then
- data.connections+=(
- typeset proto="${.sh.match[7]}"
- typeset refcnt="${.sh.match[8]}"
- typeset flags="${.sh.match[9]}"
- typeset type="${.sh.match[10]}"
- typeset state="${.sh.match[11]}"
- typeset inode="${.sh.match[12]}"
- typeset path="${.sh.match[13]}"
- )
- fi
- else
- :
- #printf "leftover=%q\n" "${leftover}"
- fi
- done <<<"$nsout"
- return 0
- }
- function list_active_local_tcp_connections
- {
- nameref ar=$1
- compound c
- integer port
- netstat_list_connections c || return 1
- # print -v c
- [[ -v ar ]] || typeset -a ar
- for i in "${!c.connections[@]}" ; do
- nameref n=c.connections[$i]
- # look for only for TCP connections which match
- # 127.0.*.* or IPv6 ::1
- if [[ "${n.proto}" == ~(El)tcp && \
- "${n.local_address}" == ~(Elr)(127\.0\..+|::1):[[:digit:]]+ ]] ; then
- port="${n.local_address##*:}"
- #printf $"port = %d\n" port
- (( ar[port]=1 ))
- fi
- done
- return 0
- }
- function main
- {
- compound c=( integer -a ar )
- list_active_local_tcp_connections c.ar || return 1
- # print -v c
- printf $"%d\n" "${!c.ar[@]}"
- return 0
- }
- # main entry point
- main
- exit $?
- # EOF.
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)
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.