-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCP-nmap-top-ports-range-selector.sh
58 lines (51 loc) · 1.43 KB
/
TCP-nmap-top-ports-range-selector.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
# Usage/help
usage() {
echo "Usage: $0 [OPTION]"
echo "Options:"
echo " -r, --range [START-END] Display ports in the range START to END, separated by commas."
echo " -t, --top [NUMBER] Display the top NUMBER of ports, separated by commas."
exit 1
}
# nmap-services file
file="/usr/share/nmap/nmap-services"
# If you don't have Nmap lol
if [[ ! -f "$file" ]]; then
echo "nmap-services file not found."
exit 1
fi
# Process arguments
if [[ $# -eq 0 ]]; then
usage
fi
while [[ $# -gt 0 ]]; do
case $1 in
-t|--top)
TOP=$2
shift # past argument
shift # past value
;;
-r|--range)
RANGE=$2
shift # past argument
shift # past value
;;
*)
usage
;;
esac
done
# Extract, sort, and format the TCP port numbers by frequency
format_output() {
# Replace newlines with commas
tr '\n' ',' | sed 's/,$/\n/'
}
if [[ ! -z "$TOP" ]]; then
awk '/tcp/ {print $2 " " $3}' "$file" | sort -rnk2 | cut -d'/' -f1 | head -n "$TOP" | format_output
elif [[ ! -z "$RANGE" ]]; then
START=$(cut -d'-' -f1 <<< "$RANGE") # Note to future self: `<<< "$RANGE"` is an alternative to `echo "$RANGE" | <etc>`
END=$(cut -d'-' -f2 <<< "$RANGE")
awk '/tcp/ {print $2 " " $3}' "$file" | sort -rnk2 | cut -d'/' -f1 | sed -n "$START,$END"p | format_output
else
usage
fi