Bash script to test port availability
I created a quick script to test if a port is consistently available between a source server to a target server for the purpose of gathering statistics to see if there are any network hiccups not.
This is a very simple and straightforward script.
Simply copy the content below to a file called test_network_port.sh
and change the permissions (e.g., chmod 700 test_network_port.sh
):
#!/bin/bash
#--------------------------------------------------------------#
# FILENAME: test_network_port.sh #
# CREATION DATE: 2021-12-06 #
# DESCRIPTION: Test network port to IP:PORT #
# AUTHOR: Ahmed Aboulnaga #
# LOG: Log file 'test_network_port.log' generated #
#--------------------------------------------------------------#
#--------------------------------------------------------------#
# Help |
#--------------------------------------------------------------#
if [ $# -ne 4 ]; then
echo ""
echo "Usage: ./test_network_port.sh [target_ip] [target_port] [number_of_loops] [delay_in_secs]"
echo ""
echo "Example: ./test_network_port.sh"
echo " ./test_network_port.sh 8 1"
echo " nohup ./test_network_port.sh 345600 0.5 &"
echo ""
exit 0
fi
#--------------------------------------------------------------#
# Check file existence |
#--------------------------------------------------------------#
if ! command -v nc &> /dev/null; then
echo ""
echo "ERROR: Command 'nc' does not exist."
echo ""
fi
#--------------------------------------------------------------#
# Parameters |
#--------------------------------------------------------------#
V_TARGETIP=${1}
V_TARGETPORT=${2}
V_LOOP=${3}
V_DELAY=${4}
#--------------------------------------------------------------#
# Loop |
#--------------------------------------------------------------#
i=1
while [ ${i} -le ${V_LOOP} ]; do
V_TIMESTAMP=`date +'%Y-%m-%d %H:%M:%S.%N'`
nc -vz ${V_TARGETIP} ${V_TARGETPORT} > test_network_port.tmp 2>&1
echo "${V_TIMESTAMP}|`cat test_network_port.tmp | tail -1 | awk '{print $9}'`" >> test_network_port.log
rm -f test_network_port.tmp
sleep ${V_DELAY}
i=`expr ${i} + 1`
done
exit 1
Pass the following arguments in this order:
- Target IP address
- Target port
- Number of times to loop
- Number of seconds between each loop (fraction of a second is allowed)
Executing it is simple (it should be run from your source server):
root@soadev:/root/temp> ./test_network_port.sh 192.168.1.1 1521 10 1
root@soadev:/root/temp>
It can also be run in the background for 2 days as shown:
root@soadev:/root/temp> ./test_network_port.sh 192.168.1.1 1521 345600 0.5
root@soadev:/root/temp>
It generates a log file called test_network_port.log
that has the following output:
2021-12-06 23:01:06.471441274|0.01
2021-12-06 23:01:07.493995977|0.01
2021-12-06 23:01:08.517366674|0.01
2021-12-06 23:01:09.540446846|0.01
2021-12-06 23:01:10.563645494|0.01
2021-12-06 23:01:11.587027598|0.01
2021-12-06 23:01:12.610403491|0.01
2021-12-06 23:01:13.633558440|0.01
2021-12-06 23:01:14.656778628|0.01
2021-12-06 23:01:15.679733290|0.01