Last active
July 21, 2022 19:29
-
-
Save raymyers/4c8e37b11deca96ac535 to your computer and use it in GitHub Desktop.
Bash script for connection drain. Uses netstat to repeatedly count established connections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
port=8080 | |
max_tries=10 | |
seconds_to_wait=3 | |
echo "Waiting for traffic on port $port to stop..." | |
for (( tries=1; tries<=$max_tries; tries++ )); do | |
# Netstat invocation tested on Ubuntu. Check your distro for differences. | |
connections=$(netstat -ant | grep ":$port.*:.*ESTABLISHED" | wc -l) | |
echo " $connections connections remaining. Try $tries/$max_tries." | |
if [ $connections -eq 0 ]; then | |
exit 0 | |
fi | |
sleep $seconds_to_wait | |
done | |
exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Waiting for traffic on port 8080 to stop... | |
8 connections remaining. Try 1/10. | |
8 connections remaining. Try 2/10. | |
7 connections remaining. Try 3/10. | |
3 connections remaining. Try 4/10. | |
0 connections remaining. Try 5/10. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment