Skip to content

Instantly share code, notes, and snippets.

@raymyers
Last active July 21, 2022 19:29
Show Gist options
  • Save raymyers/4c8e37b11deca96ac535 to your computer and use it in GitHub Desktop.
Save raymyers/4c8e37b11deca96ac535 to your computer and use it in GitHub Desktop.
Bash script for connection drain. Uses netstat to repeatedly count established connections
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
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