Difference between
modified post d0b0a by Anonymous on Sun 26th Feb 2023 17:57
original post 681dac5 by Anonymous on Sun 26th Feb 2023 17:31
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | #!/usr/bin/ksh93 | |
| 2 | 2 | ||
| 3 | 3 | # | |
| 4 | 4 | # find_free_tcp_port1.ksh - find a free tcp port | |
| 5 | 5 | # | |
| 6 | 6 | # Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 7 | 7 | # | |
| 8 | 8 | ||
| 9 | 9 | # | |
| 10 | 10 | # simple netstat -n parser | |
| 11 | 11 | # | |
| 12 | 12 | function netstat_list_connections | |
| 13 | 13 | { | |
| 14 | + | # fixme: should be set -o nounset clean | |
| 14 | 15 | nameref data=$1 | |
| 15 | 16 | ||
| 16 | 17 | typeset -a data.connections | |
| 17 | 18 | ||
| 18 | 19 | typeset nsout="$(LC_ALL='POSIX' netstat -n)" | |
| 19 | 20 | ||
| 20 | 21 | while read i ; do | |
| 21 | 22 | leftover="${i/~(Elrx) | |
| 22 | 23 | (?: # non-capturing group | |
| 23 | 24 | # | |
| 24 | 25 | # regex group for tcpudp | |
| 25 | 26 | # | |
| 26 | 27 | (tcp|tcp6|udp) # Proto | |
| 27 | 28 | [[:space:]]+ | |
| 28 | 29 | ([[:digit:]]+) # Recv-Q | |
| 29 | 30 | [[:space:]]+ | |
| 30 | 31 | ([[:digit:]]+) # Send-Q | |
| 31 | 32 | [[:space:]]+ | |
| 32 | 33 | ([^[:space:]]+) # Local Address | |
| 33 | 34 | [[:space:]]+ | |
| 34 | 35 | ([^[:space:]]+) # Foreign Address | |
| 35 | 36 | [[:space:]]+ | |
| 36 | 37 | ([^[:space:]]+) # State | |
| 37 | 38 | | | |
| 38 | 39 | # | |
| 39 | 40 | # regex for unix | |
| 40 | 41 | # | |
| 41 | 42 | (unix) # Proto | |
| 42 | 43 | [[:space:]]+ | |
| 43 | 44 | ([[:digit:]]+) # RefCnt | |
| 44 | 45 | [[:space:]]+ | |
| 45 | 46 | (\[.+\]) # Flags | |
| 46 | 47 | [[:space:]]+ | |
| 47 | 48 | ([^[:space:]]+) # Type | |
| 48 | 49 | [[:space:]]+ | |
| 49 | 50 | ([^[:space:]]*?) # State | |
| 50 | 51 | [[:space:]]+ | |
| 51 | 52 | ([[:digit:]]+) # I-Node | |
| 52 | 53 | (?: | |
| 54 | + | # Path is optional | |
| 53 | 55 | | | |
| 54 | 56 | [[:space:]]+ | |
| 55 | 57 | ([^[:space:]]+) # Path | |
| 56 | 58 | ) | |
| 57 | 59 | ) | |
| 58 | 60 | /X}" | |
| 59 | 61 | ||
| 60 | - | # if the regex above did not match then .sh.match remains | |
| 62 | + | # if the regex above did not match then .sh.match | |
| 61 | - | # untouched. | |
| 63 | + | # remains untouched, so we might see data from the | |
| 62 | - | # so we check the leftover whether it just contains the | |
| 64 | + | # previous round. | |
| 63 | - | # dummy value of "X" to indicate a successful regex match | |
| 65 | + | # so we check the "leftover" var whether it just | |
| 66 | + | # contains the dummy value of "X" to indicate a | |
| 64 | 68 | if [[ "$leftover" == 'X' ]] ; then | |
| 65 | 69 | #print -v .sh.match | |
| 66 | 70 | ||
| 67 | 71 | if [[ "${.sh.match[1]}" != '' ]] ; then | |
| 68 | 72 | data.connections+=( | |
| 69 | 73 | typeset proto="${.sh.match[1]}" | |
| 70 | 74 | typeset recv_q="${.sh.match[2]}" | |
| 71 | 75 | typeset send_q="${.sh.match[3]}" | |
| 72 | 76 | typeset local_address="${.sh.match[4]}" | |
| 73 | 77 | typeset foreign_address="${.sh.match[5]}" | |
| 74 | 78 | typeset state="${.sh.match[6]}" | |
| 75 | 79 | ) | |
| 76 | 80 | elif [[ "${.sh.match[7]}" != '' ]] ; then | |
| 77 | 81 | data.connections+=( | |
| 78 | 82 | typeset proto="${.sh.match[7]}" | |
| 79 | 83 | typeset refcnt="${.sh.match[8]}" | |
| 80 | 84 | typeset flags="${.sh.match[9]}" | |
| 81 | 85 | typeset type="${.sh.match[10]}" | |
| 82 | 86 | typeset state="${.sh.match[11]}" | |
| 83 | 87 | typeset inode="${.sh.match[12]}" | |
| 84 | 88 | typeset path="${.sh.match[13]}" | |
| 85 | 89 | ) | |
| 86 | 90 | fi | |
| 87 | 91 | else | |
| 88 | 92 | : | |
| 89 | - | #printf "leftover=%q\n" "${leftover}" | |
| 93 | + | printf "leftover=%q\n" "${leftover}" | |
| 90 | 94 | fi | |
| 91 | 95 | done <<<"$nsout" | |
| 92 | 96 | ||
| 93 | 97 | return 0 | |
| 94 | 98 | } | |
| 95 | 99 | ||
| 96 | 100 | function main | |
| 97 | 101 | { | |
| 98 | 102 | compound c | |
| 99 | 103 | netstat_list_connections c | |
| 100 | - | # print -v c | |
| 104 | + | print -v c | |
| 101 | 105 | #set -x | |
| 102 | 106 | for i in "${!c.connections[@]}" ; do | |
| 103 | 107 | nameref n=c.connections[$i] | |
| 104 | 108 | if [[ "${n.proto}" == ~(El)tcp && "${n.local_address}" == ~(Elr)(127\.0\..+|::1):[[:digit:]]+ ]] ; then | |
| 105 | 109 | #print -v n | |
| 106 | 110 | printf $"port = %q\n" "${n.local_address##*:}" | |
| 107 | 111 | fi | |
| 108 | 112 | done | |
| 109 | 113 | ||
| 110 | 114 | } | |
| 111 | 115 | ||
| 112 | 116 | main | |
| 113 | 117 | # EOF. | |
| 114 | 118 |
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.
rovema.kpaste.net RSS