Skip to content

Instantly share code, notes, and snippets.

@neuthral
Last active November 25, 2022 22:17
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 neuthral/0ff41380958321d69888fead29d510ee to your computer and use it in GitHub Desktop.
Save neuthral/0ff41380958321d69888fead29d510ee to your computer and use it in GitHub Desktop.
Loop through list of trackers and test if they are alive using nc
#!/bin/bash
##
# Loop through list of trackers and test if they are alive using nc
# nc -v -z -w 3 -u "$host" "$port"
##
usage () {
printf '\n%s\n' "Loop through list of trackers and test if they are alive using nc"
printf '\n%s\n\n' "Usage: $(basename "$0") [list-of-trackers.file]"
exit
}
if [[ ! -f $1 ]]; then
usage
fi
while read -r line; do
FIELDS=($(echo $line | grep -o "[a-z0-9.-]*" | tr '\n' ' '))
if [[ ${FIELDS[0]} == "udp" ]]; then
# set udp flag if protocol is udp
args="-z -v -w 1 -u"
else
args="-z -v -w 1"
fi
if [[ ! ${FIELDS[1]} ]]; then
echo '-'
else
if nc $( echo "$args ${FIELDS[1]} ${FIELDS[2]}"); then
echo "$line" >> alive.txt
fi
fi
done <"$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment