Skip to content

Commit d59abca

Browse files
committed
fix: fixed port check issues
1 parent 293d12d commit d59abca

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

containers/check-ports.sh

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,29 @@ check_port() {
4646
local optional=${3:-false}
4747
local os=$(detect_os)
4848
local port_in_use=false
49-
5049
case $os in
5150
"windows")
52-
# Use netstat on Windows (available by default)
53-
if netstat -an | grep -q ":$port "; then
51+
# Only consider LISTENING state
52+
if netstat -an | grep -E ":$port[[:space:]]" | grep -iq "LISTEN"; then
5453
port_in_use=true
5554
fi
5655
;;
5756
"macos")
58-
# Use netstat on macOS (available by default)
59-
if netstat -an | grep -q "\\.$port "; then
57+
# Only consider LISTEN state
58+
if netstat -an | grep -E "\.$port[[:space:]]" | grep -iq "LISTEN"; then
6059
port_in_use=true
6160
fi
6261
;;
6362
"linux")
64-
# Try multiple methods on Linux
6563
if command -v ss &> /dev/null; then
66-
# Use ss (modern replacement for netstat)
67-
if ss -tuln | grep -q ":$port "; then
64+
if ss -tuln | awk '{print $4,$1}' | grep -E "[:.]$port[[:space:]]" | grep -iq "LISTEN"; then
6865
port_in_use=true
6966
fi
7067
elif command -v netstat &> /dev/null; then
71-
# Fall back to netstat
72-
if netstat -tuln | grep -q ":$port "; then
68+
if netstat -tuln | grep -E ":$port[[:space:]]" | grep -iq "LISTEN"; then
7369
port_in_use=true
7470
fi
7571
elif command -v nc &> /dev/null; then
76-
# Fall back to netcat
7772
if nc -z localhost $port 2>/dev/null; then
7873
port_in_use=true
7974
fi
@@ -83,7 +78,6 @@ check_port() {
8378
fi
8479
;;
8580
*)
86-
# Unknown OS - try netcat if available
8781
if command -v nc &> /dev/null; then
8882
if nc -z localhost $port 2>/dev/null; then
8983
port_in_use=true

containers/check-sonarqube-ports.sh

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,32 @@ detect_os() {
3434
check_port() {
3535
local port=$1
3636
local description=$2
37+
local optional=${3:-false}
3738
local os=$(detect_os)
3839
local port_in_use=false
39-
4040
case $os in
4141
"windows")
42-
# Use netstat on Windows (available by default)
43-
if netstat -an | grep -q ":$port "; then
42+
# Only consider LISTENING state
43+
if netstat -an | grep -E ":$port[[:space:]]" | grep -iq "LISTEN"; then
4444
port_in_use=true
4545
fi
4646
;;
4747
"macos")
48-
# Use netstat on macOS (available by default)
49-
if netstat -an | grep -q "\\.$port "; then
48+
# Only consider LISTEN state
49+
if netstat -an | grep -E "\.$port[[:space:]]" | grep -iq "LISTEN"; then
5050
port_in_use=true
5151
fi
5252
;;
5353
"linux")
54-
# Try multiple methods on Linux
5554
if command -v ss &> /dev/null; then
56-
# Use ss (modern replacement for netstat)
57-
if ss -tuln | grep -q ":$port "; then
55+
if ss -tuln | awk '{print $4,$1}' | grep -E "[:.]$port[[:space:]]" | grep -iq "LISTEN"; then
5856
port_in_use=true
5957
fi
6058
elif command -v netstat &> /dev/null; then
61-
# Fall back to netstat
62-
if netstat -tuln | grep -q ":$port "; then
59+
if netstat -tuln | grep -E ":$port[[:space:]]" | grep -iq "LISTEN"; then
6360
port_in_use=true
6461
fi
6562
elif command -v nc &> /dev/null; then
66-
# Fall back to netcat
6763
if nc -z localhost $port 2>/dev/null; then
6864
port_in_use=true
6965
fi
@@ -73,7 +69,6 @@ check_port() {
7369
fi
7470
;;
7571
*)
76-
# Unknown OS - try netcat if available
7772
if command -v nc &> /dev/null; then
7873
if nc -z localhost $port 2>/dev/null; then
7974
port_in_use=true
@@ -84,6 +79,10 @@ check_port() {
8479
fi
8580
;;
8681
esac
82+
return 0
83+
fi
84+
;;
85+
esac
8786

8887
if [ "$port_in_use" = true ]; then
8988
echo -e "${RED}❌ PORT $port ($description) is already in use${NC}"

0 commit comments

Comments
 (0)