Skip to content

Instantly share code, notes, and snippets.

@mgor
Created April 1, 2020 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgor/1d745e2085f8a9ec9412b868299a208a to your computer and use it in GitHub Desktop.
Save mgor/1d745e2085f8a9ec9412b868299a208a to your computer and use it in GitHub Desktop.
Check for open ports in a firewall
#!/usr/bin/env bash
main() {
local first=$1
local last=$2
local pids=()
[[ -z "${first}" || -z "${last}" ]] && { >&2 echo "listen.sh <first> <last>"; return 1; }
for (( port=first; port <= last; port++ )); do
if netstat -an | grep -qE ":${port} .*LISTEN"; then
echo "skipping ${port}"
continue
fi
nc -l $port &
local pid=$!
pids+=("${pid}")
done
echo "press any key to stop"
read -r done
for pid in "${pids[@]}"; do
kill -9 $pid &> /dev/null
done
return 0
}
main "$@"
#!/usr/bin/env bash
main() {
local host=$1
local first=$2
local last=$3
for (( port=first; port <= last; port++ )); do
ncat "${host}" "${port}" -c "echo ${port} open"
sleep $((RANDOM%5+1))
done
return 0
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment