pastebin - collaborative debugging tool
rovema.kpaste.net RSS

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

    
11
#!/usr/bin/ksh93
22
33
#
44
# find_free_tcp_port1.ksh - find a free tcp port
55
#
66
# Written by Roland Mainz <roland.mainz@nrubsig.org>
77
#
88
99
#
1010
# simple netstat -n parser
1111
#
1212
function netstat_list_connections
1313
{
14+
# fixme: should be set -o nounset clean
1415
	nameref data=$1
1516
1617
	typeset -a data.connections
1718
1819
	typeset nsout="$(LC_ALL='POSIX' netstat -n)"
1920
2021
	while read i ; do
2122
		leftover="${i/~(Elrx)
2223
		(?: # non-capturing group
2324
			#
2425
			# regex group for tcpudp
2526
			#
2627
			(tcp|tcp6|udp)	# Proto
2728
			[[:space:]]+
2829
			([[:digit:]]+)			# Recv-Q
2930
			[[:space:]]+
3031
			([[:digit:]]+)			# Send-Q
3132
			[[:space:]]+
3233
			([^[:space:]]+)			# Local Address
3334
			[[:space:]]+
3435
			([^[:space:]]+)			# Foreign Address
3536
			[[:space:]]+
3637
			([^[:space:]]+) 		# State
3738
		|
3839
			#
3940
			# regex for unix
4041
			#
4142
			(unix)				# Proto
4243
			[[:space:]]+
4344
			([[:digit:]]+)			# RefCnt
4445
			[[:space:]]+
4546
			(\[.+\])			# Flags
4647
			[[:space:]]+
4748
			([^[:space:]]+) 		# Type
4849
			[[:space:]]+
4950
			([^[:space:]]*?)		# State
5051
			[[:space:]]+
5152
			([[:digit:]]+)			# I-Node
5253
			(?:
54+
				# Path is optional
5355
				|
5456
				[[:space:]]+
5557
				([^[:space:]]+)		# Path
5658
			)
5759
		)
5860
			/X}"
5961
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
6468
		if [[ "$leftover" == 'X' ]] ; then
6569
			#print -v .sh.match
6670
6771
			if [[ "${.sh.match[1]}" != '' ]] ; then
6872
				data.connections+=(
6973
					typeset proto="${.sh.match[1]}"
7074
					typeset recv_q="${.sh.match[2]}"
7175
					typeset send_q="${.sh.match[3]}"
7276
					typeset local_address="${.sh.match[4]}"
7377
					typeset foreign_address="${.sh.match[5]}"
7478
					typeset state="${.sh.match[6]}"
7579
				)
7680
			elif [[ "${.sh.match[7]}" != '' ]] ; then
7781
				data.connections+=(
7882
					typeset proto="${.sh.match[7]}"
7983
					typeset refcnt="${.sh.match[8]}"
8084
					typeset flags="${.sh.match[9]}"
8185
					typeset type="${.sh.match[10]}"
8286
					typeset state="${.sh.match[11]}"
8387
					typeset inode="${.sh.match[12]}"
8488
					typeset path="${.sh.match[13]}"
8589
				)
8690
			fi
8791
		else
8892
			:
89-
			#printf "leftover=%q\n" "${leftover}"
93+
			printf "leftover=%q\n" "${leftover}"
9094
		fi
9195
	done <<<"$nsout"
9296
9397
	return 0
9498
}
9599
96100
function main
97101
{
98102
	compound c
99103
	netstat_list_connections c
100-
#	print -v c
104+
	print -v c
101105
#set -x
102106
	for i in "${!c.connections[@]}" ; do
103107
		nameref n=c.connections[$i]
104108
		if [[ "${n.proto}" == ~(El)tcp && "${n.local_address}" == ~(Elr)(127\.0\..+|::1):[[:digit:]]+ ]] ; then
105109
			#print -v n
106110
			printf $"port = %q\n" "${n.local_address##*:}"
107111
		fi
108112
	done
109113
110114
}
111115
112116
main
113117
# EOF.
114118

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