Skip to content

Instantly share code, notes, and snippets.

@JFFail
Last active July 31, 2020 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JFFail/0bf09eba6ea20e69384e to your computer and use it in GitHub Desktop.
Save JFFail/0bf09eba6ea20e69384e to your computer and use it in GitHub Desktop.
Script to repeatedly check port availability on an endpoint. Takes server name and a port number as parameters.
#!/bin/bash
if [[ $# -ne 2 ]]; then
echo "Please submit 2 arguments: server name and port number"
else
total=0
success=0
while true; do
total=$((total+1))
result=$(nmap -Pn -p $2 $1 | grep "/tcp")
echo -n $result
open=$(echo $result | awk '{print $2}')
if [[ $open == "open" ]]; then
success=$((success+1))
fi
length=$(echo $result | wc -c)
if [[ $total -eq 1 ]]; then
baseline=$length
elif [[ $length -lt $baseline ]]; then
baseline=$length
fi
percent=$(echo "scale=2;100*$success/$total" | bc)
if [[ $length -gt $baseline ]]; then
echo -e " > \t$success / $total - $percent% "
else
echo -e " > \t\t$success / $total - $percent%"
fi
sleep 5
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment