pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post fd80 by Anonymous on Sun 26th Feb 2023 21:39
original post d0b0a by Anonymous on Sun 26th Feb 2023 17:57
Download diff
Show old version | new version | both versions

    
11
#!/usr/bin/ksh93
22
33
#
4-
# find_free_tcp_port1.ksh - find a free tcp port
4+
# list_active_local_tcp_ports1.ksh - list port numbers of local TCP connections
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
{
1414
# fixme: should be set -o nounset clean
1515
	nameref data=$1
1616
1717
	typeset -a data.connections
1818
1919
	typeset nsout="$(LC_ALL='POSIX' netstat -n)"
2020
2121
	while read i ; do
2222
		leftover="${i/~(Elrx)
2323
		(?: # non-capturing group
2424
			#
2525
			# regex group for tcpudp
2626
			#
2727
			(tcp|tcp6|udp)	# Proto
2828
			[[:space:]]+
2929
			([[:digit:]]+)			# Recv-Q
3030
			[[:space:]]+
3131
			([[:digit:]]+)			# Send-Q
3232
			[[:space:]]+
3333
			([^[:space:]]+)			# Local Address
3434
			[[:space:]]+
3535
			([^[:space:]]+)			# Foreign Address
3636
			[[:space:]]+
3737
			([^[:space:]]+) 		# State
3838
		|
3939
			#
4040
			# regex for unix
4141
			#
4242
			(unix)				# Proto
4343
			[[:space:]]+
4444
			([[:digit:]]+)			# RefCnt
4545
			[[:space:]]+
4646
			(\[.+\])			# Flags
4747
			[[:space:]]+
4848
			([^[:space:]]+) 		# Type
4949
			[[:space:]]+
5050
			([^[:space:]]*?)		# State
5151
			[[:space:]]+
5252
			([[:digit:]]+)			# I-Node
5353
			(?:
5454
				# Path is optional
5555
				|
5656
				[[:space:]]+
5757
				([^[:space:]]+)		# Path
5858
			)
5959
		)
6060
			/X}"
6161
6262
		# if the regex above did not match then .sh.match
6363
		# remains untouched, so we might see data from the
6464
		# previous round.
6565
		# so we check the "leftover" var whether it just
6666
		# contains the dummy value of "X" to indicate a
6767
		# successful regex match
6868
		if [[ "$leftover" == 'X' ]] ; then
6969
			#print -v .sh.match
7070
7171
			if [[ "${.sh.match[1]}" != '' ]] ; then
7272
				data.connections+=(
7373
					typeset proto="${.sh.match[1]}"
7474
					typeset recv_q="${.sh.match[2]}"
7575
					typeset send_q="${.sh.match[3]}"
7676
					typeset local_address="${.sh.match[4]}"
7777
					typeset foreign_address="${.sh.match[5]}"
7878
					typeset state="${.sh.match[6]}"
7979
				)
8080
			elif [[ "${.sh.match[7]}" != '' ]] ; then
8181
				data.connections+=(
8282
					typeset proto="${.sh.match[7]}"
8383
					typeset refcnt="${.sh.match[8]}"
8484
					typeset flags="${.sh.match[9]}"
8585
					typeset type="${.sh.match[10]}"
8686
					typeset state="${.sh.match[11]}"
8787
					typeset inode="${.sh.match[12]}"
8888
					typeset path="${.sh.match[13]}"
8989
				)
9090
			fi
9191
		else
9292
			:
93-
			printf "leftover=%q\n" "${leftover}"
93+
			#printf "leftover=%q\n" "${leftover}"
9494
		fi
9595
	done <<<"$nsout"
9696
9797
	return 0
9898
}
9999
100-
function main
100+
function list_active_local_tcp_connections
101101
{
102+
	nameref ar=$1
102103
	compound c
103-
	netstat_list_connections c
104+
	integer port
113105
114-
}
115106
116-
main
107+
	netstat_list_connections c || return 1
117-
# EOF.
108+
#	print -v c
118109
119-
110+
	[[ -v ar ]] || typeset -a ar
120111
121-
112+
	for i in "${!c.connections[@]}" ; do
122-
113+
		nameref n=c.connections[$i]
123114
124-
115+
		# look for only for TCP connections which match
125-
116+
		# 127.0.*.* or IPv6 ::1
126-
117+
		if [[ "${n.proto}" == ~(El)tcp && \
127-
118+
			"${n.local_address}" == ~(Elr)(127\.0\..+|::1):[[:digit:]]+ ]] ; then
128119
129-
120+
			port="${n.local_address##*:}"
130-
121+
			#printf $"port = %d\n" port
131122
132-
123+
			(( ar[port]=1 ))
133-
124+
		fi
134-
125+
	done
135126
136-
127+
	return 0
137-
128+
}
138129
139-
130+
function main
140-
131+
{
141-
132+
	compound c=( integer -a ar )
142133
143-
134+
	list_active_local_tcp_connections c.ar || return 1
144135
145-
136+
#	print -v c
146-
137+
	printf $"%d\n" "${!c.ar[@]}"
147138
148-
139+
	return 0
149-
140+
}
150141
151-
142+
# main entry point
152-
143+
main
153-
144+
exit $?
154-
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