Skip to content

Instantly share code, notes, and snippets.

@ceylanb
Created September 28, 2023 07:26
Show Gist options
  • Save ceylanb/d1f06f6b8ae8cf57bef508cc4c98feed to your computer and use it in GitHub Desktop.
Save ceylanb/d1f06f6b8ae8cf57bef508cc4c98feed to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
# Help message function
print_help() {
echo "Usage: $0 <TYPE> <NUM> <SEP>"
echo " TYPE: Port type to filter (e.g. udp, tcp)"
echo " NUM: Number of results to retrieve"
echo " SEP: Seperator (e.g. , or .)"
}
# Check if the number of arguments is correct
if [ "$#" -lt 2 ]; then
echo "Error: Invalid number of arguments"
print_help
exit 1
fi
TYPE="$1"
NUM="$2"
SEP="$3"
# Retrieve the matching ports and process them
#out=$(grep "/$TYPE" /usr/local/opt/nmap/share/nmap/nmap-services | sort -rk3 | cut -f2 | grep -Eo '\d+' | head -n "$NUM" | tr ',' '\n' | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }')
out=$(grep "/$TYPE" /usr/local/opt/nmap/share/nmap/nmap-services | sort -rk3 | cut -f2 | grep -Eo '\d+' | head -n "$NUM")
# Check if the first argument is not empty
if [ -n "$3" ]; then
out=$(printf "$out" | tr '\n' "$3")
fi
# Remove trailing comma from the output
out="${out%,}"
echo "$out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment