Skip to content

Instantly share code, notes, and snippets.

@mgor

mgor/listen.bash

Created Apr 1, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.