-
Notifications
You must be signed in to change notification settings - Fork 0
/
findBox
executable file
·117 lines (113 loc) · 3.23 KB
/
findBox
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
#
# findBox
#
# (c) Miguel Leitao, 2016
# Part of meoRemote: https://github.com/miguelleitao/meoRemote
#
# Scans network for available meoBoxes.
#
# Usage: ./findBox [-q] [-c] [NetMaskLenLimit [NetAddr]]
# NetAddr defaults to addresses from all available net interfaces.
# NetMaskLenLimit defaults to 24. Use 28 for a norrow (faster) search.
# -q: quiet mode (no header)
# -d: debug mode
# -s: single mode. Only show first found box.
# -c: config file mode. Use ./findBox -c >appname.conf
#
debug=0
nmap_exe="nmap"
nmap_cmd="$nmap_exe -n -Pn -p T:8082 --max-hostgroup 64 --open -oG -"
if ! command -v $nmap_exe &> /dev/null
then
echo "Error: $nmap_exe could not be found."
exit
fi
mask2cdr ()
{
local x=${1##*255.}
set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
x=${1%%$3*}
echo $(( $2 + (${#x}/4) ))
}
cdr2mask ()
{
set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
[ $1 -gt 1 ] && shift $1 || shift
echo ${1-0}.${2-0}.${3-0}.${4-0}
}
network_addr()
{
# Calculate network address from address and prefix len
# Usage: naddr = `network_mask ip_addr mask_len`
local nmask=`cdr2mask $2`
local ia1; local ia2; local ia3; local ia4
IFS=. read -r ia1 ia2 ia3 ia4 <<< "$1"
IFS=. read -r im1 im2 im3 im4 <<< "$nmask"
local naddr="$((ia1 & im1)).$((ia2 & im2)).$((ia3 & im3)).$((ia4 & im4))"
echo $naddr
}
masklen=24
if [ "$1" == "-q" ]; then
quiet=1
shift
fi
if [ "$1" == "-c" ]; then
quiet=1
conf=1
#" |awk -c '{print \"box \" $5}"
shift
fi
if [ "$1" == "-d" ]; then
debug=1
shift
fi
if [ "$1" == "-s" ]; then
single=1
shift
fi
[ "$1" != "" ] && masklen=$1
[ "$quiet" != "1" ] && echo "# meoBoxes found:"
addrs=`ifconfig | grep -e 'inet .*' |grep -v 127.0.0.1 |cut -f2 -dt |cut -d' ' -f2`
[ "$2" != "" ] && addrs=$2
#echo "addrs: " $addrs
for i in $addrs; do
[ "$i" = "192.168.122.1" ] && continue
[ "$debug" = 1 ] && echo "local address: "$i
nmask=`ifconfig |grep inet |grep $i |cut -f3 -dt |cut -f2 -dk |cut -d' ' -f2`
[ "$debug" = 1 ] && echo "netmask: "$nmask
min_cidr=`mask2cdr $nmask`
[ "$debug" = 1 ] && echo "min_cidr: "$min_cidr
cidr=$(( $masklen > $min_cidr ? $masklen : $min_cidr ))
[ "$debug" = 1 ] && echo "cidr: "$cidr
if [ "$single" = 1 ]; then
excludes=$i
for scidr in `seq 31 -1 $cidr`; do
naddr=`network_addr $i $scidr`
[ $debug = 1 ] && echo "netaddr: "$naddr
cmd="$nmap_cmd --exclude $excludes $naddr/$scidr"
[ "$debug" = 1 ] && echo "cmd: "$cmd
if [ "$conf" != "1" ]; then
result=`$cmd |grep open/tcp | cut -f2 -d' '`
else
result=`$cmd |grep open/tcp | awk -c '{print "box " $2}'`
fi
size=${#result}
if [ "$size" -gt 10 ]; then
echo $result
exit;
fi
excludes="$naddr/$scidr"
done
else
naddr=`network_addr $i $cidr`
[ $debug = 1 ] && echo "netaddr: "$naddr
cmd="$nmap_cmd --exclude $i $naddr/$cidr"
[ "$debug" = 1 ] && echo "cmd: "$cmd
if [ "$conf" != "1" ]; then
$cmd |grep open/tcp | cut -f2 -d' '
else
$cmd |grep open/tcp | awk -c '{print "box " $2}'
fi
fi
done