Skip to content

Commit c6e810d

Browse files
committed
add port testing script
1 parent 742a436 commit c6e810d

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

testing/open-ports.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash
2+
3+
declare -a ports=("4505" "4506")
4+
_SETUP_PORTS=False
5+
_TEST_PORTS=False
6+
7+
while getopts "i:st" opt; do
8+
case $opt in
9+
i)
10+
HOST=${OPTARG}
11+
echo "Testing ports on host ${HOST}" >&2
12+
;;
13+
s)
14+
_SETUP_PORTS=True
15+
echo "Setting up listening ports" >&2
16+
;;
17+
t)
18+
_TEST_PORTS=True
19+
echo "Testing ports on host ${HOST}" >&2
20+
;;
21+
\?)
22+
echo "Invalid option: -$OPTARG" >&2
23+
;;
24+
esac
25+
done
26+
27+
function result {
28+
RESULT=$1
29+
PORT=$2
30+
case $RESULT in
31+
0)
32+
echo "The access to $PORT was successful"
33+
;;
34+
1)
35+
echo "The access to $PORT failed"
36+
exit
37+
;;
38+
*)
39+
echo "Unknown exit error for the $PORT command. Exiting script."
40+
exit
41+
;;
42+
esac
43+
}
44+
45+
function test_ports {
46+
for port in "${ports[@]}"; do
47+
test_port=$(nc -z ${HOST} ${port})
48+
rc=$?
49+
result $rc port_${port}
50+
done
51+
}
52+
53+
function start_ports {
54+
for port in ${ports}; do
55+
test_port=$(nc -lk ${port} &)
56+
done
57+
}
58+
59+
if [ ${_SETUP_PORTS} = True ]; then
60+
start_ports
61+
elif [ ${_TEST_PORTS} = True ]; then
62+
test_ports
63+
fi

0 commit comments

Comments
 (0)