/
openports.sh
executable file
·62 lines (55 loc) · 1.28 KB
/
openports.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
59
60
61
62
#!/bin/bash
# openports.sh
# Prints out all hosts that have the specified port(s) open in gnmap file.
# By Sebastien Jeanquier (@securitygen)
# Security Generation - http://www.securitygeneration.com
#
# ChangeLog -
# 0.1: Initial release
#
# -------------------------------------------------------------
function usage {
echo "Description: Lists hosts with a given port open."
echo "Usage: $0 [-i] {port} {gnmap file}"
echo " Port can be single port (e.g. 22), or multiple '21|22|23' (note quoted list)"
echo " Optional -i outputs only IP addresses"
}
if [ $# -eq 0 ];
then
usage "$0";
exit 1
else
iflag=false
while getopts i opt
do
case "$opt" in
i)
iflag=true
;;
\?) # unknown flag
usage "$0"
exit 1
;;
esac
done
shift "$((OPTIND-1))"
# Assign parameters to variables
port=$1
gnmapfile=$2
# Check port entry is valid
re="^[0-9]+(\|[0-9]+)*$"
if ! [[ $port =~ $re ]] ; then
echo "Error: port number must be an integer or '23|24|...', you entered: $port" >&2; exit 1
exit 1
fi
# Check gnmap file exists
if [[ ! -f $gnmapfile ]]; then
echo "Error: File '$gnmapfile' doesn't exist."
exit 1
fi
if [[ $iflag == true ]]; then
egrep "\s($port)\/open" "$gnmapfile" | awk '{print $2}'
else
egrep "\s($port)\/open" "$gnmapfile"
fi
fi #echo