I am trying to match a line in netstat
where the local port matches some port that's passed in to my script. But this regex (while it matches in a regex tester) doesn't seem to work.
# Get lines from netstat
IFS=$'\r\n' GLOBIGNORE='*' command eval 'NetstatLines=($(netstat -anq))'
for line in "${NetstatLines[@]}"; do
# This never matches
if [[ "$line" =~ ^\s*(TCP|UDP)\s+(\d+\.\d+\.\d+\.\d+:$iPort|\[::\d*\]:$iPort)\s+ ]]; then
echo "found it!!"
fi
done
The lines look like:
Active Connections
Proto Local Address Foreign Address State
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:623 0.0.0.0:0 LISTENING
TCP 0.0.0.0:2179 0.0.0.0:0 LISTENING
TCP 0.0.0.0:5040 0.0.0.0:0 LISTENING
TCP 0.0.0.0:7680 0.0.0.0:0 LISTENING
So if $iPort
is 135
, it should match the 4th line.