-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathah-ranges.sh
executable file
·189 lines (159 loc) · 4.88 KB
/
ah-ranges.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
# Copyright (C) 2018 Stephen Farrell, [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#set -x
# Create a diretory with content specific to an asset-holder
# idea is we have a set of ranges that allows us to extract
# the set of collision info that we might wanna send to an
# asset-holder
# Note that manual examination is needed before makging a
# tarball and sending - the prefixes might select too
# many clusters
# this is based on ah-tb.sh
function whenisitagain()
{
date -u +%Y%m%d-%H%M%S
}
NOW=$(whenisitagain)
outdir="ah-"$NOW
function howmany() {
case $- in *f*) set -- $1;; *) set -f; set -- $1; set +f;; esac
echo $#
}
function copyif()
{
# copy $1 if it exists to $2
if [ -f $1 ]
then
cp $1 $2
fi
}
function i2dq()
{
ival=$1
dq4=$((ival/(256*256*256)))
dq3=$(((ival/(256*256))%256))
dq2=$(((ival/256)%256))
dq1=$((ival%256))
# without the '"ip": ' prefix here, we suck in IPs found for SANs in certs
# probably better to not do that (though interesting such overlaps exist!?)
echo "\"ip\": \"$dq4.$dq3.$dq2.$dq1\""
}
function range2list()
{
of=$2
for i in $1; do
# dotted quad, split into values
A4="${i%-*}"; B4="${i#*-}"
a4=${A4/.*}; b4=${B4/.*};
A3="${A4#*.}"; B3="${B4#*.}"
a3=${A3/.*}; b3=${B3/.*};
A2="${A3#*.}"; B2="${B3#*.}"
a2=${A2/.*}; b2=${B2/.*};
A1="${A2#*.}"; B1="${B2#*.}"
a1=${A1/.*}; b1=${B1/.*};
# map to ints
min=$((a1+a2*256+a3*(256*256)+a4*(256*256*256)))
max=$((b1+b2*256+b3*(256*256)+b4*(256*256*256)))
# print needed
for ((i = min; i<= max; i++ ))
do
# map back to dotted quad
i2dq $i >>$of
done
done
}
# testy test...
#range2list "80.93.28.0-80.93.29.255" /dev/stdout
#range2list "92.51.242.254-92.51.243.4" /dev/stdout
#exit 0
rangefile=""
rundir=""
usage()
{
echo "Extract a set of clusters that match a search string/regexp to sending to asset-holder"
echo "usage: $0 <range-file> <run-dir>"
echo " results will be in a directory named with a timestamp, e.g. $outdir"
echo " you might want to give that a more meaningful name"
echo " the rundir contains the cluster files"
echo " range file is a JSON file extracted from RIPE"
exit 99
}
if (( $# != 2 ))
then
usage
fi
rangefile=$1
rundir=$2
if [ ! -f $rangefile ]
then
echo "Can't read $rangefile - exiting "
echo "The RIPE DB may help here. Try using wget with s/XXX.XXX.XXX.XXX/your-ip/ as follows:"
echo " wget 'https://rest.db.ripe.net/search.json?query-string=XXX.XXX.XXX.XXX&flags=no-filtering&source=RIPE' "
echo "Or maybe using a hoster name (if it exists in the RIPE DB) is better"
echo "Bear in mind there's an AUP, so don't flood that."
exit 1
fi
if [ ! -d $rundir ]
then
echo "No directory $rundir - exiting."
exit 2
fi
# plan:
# grep out ranges from input
# generate IPs from ranges to ipaddrs
# then use grep
rawranges=`grep -A1 inetnum $rangefile | grep value | sort -V | uniq`
ranges=`echo "$rawranges" | awk '{print $3"-"$5}' | sed -e 's/"//g'`
tmpf=`mktemp /tmp/ahrangesXXXX`
for range in $ranges
do
#echo "range: $range"
range2list $range $tmpf
done
matchingfiles=`grep -l -F -f $tmpf $rundir/cluster*.json`
matchingcount=$(howmany "$matchingfiles")
if (( matchingcount==0))
then
echo "There are $matchingcount clusters matching - exiting"
rm $tmpf
exit 0
fi
echo "There are $matchingcount clusters matching "
mkdir -p $outdir
if [ ! -d $outdir ]
then
echo "Can't make $outdir - exiting"
exit 1
fi
mv $tmpf $outdir/allipaddrs.txt
for cluster in $matchingfiles
do
echo "Copying over info about $cluster"
cnum=`basename $cluster .json | sed -e 's/cluster//'`
cp $rundir/cluster$cnum.json $outdir
copyif $rundir/cluster$cnum.words $outdir
copyif $rundir/graph$cnum.dot $outdir
copyif $rundir/graph$cnum.dot.svg $outdir
copyif $rundir/graph$cnum.dot.png $outdir
copyif $rundir/cluster$cnum-wordle.png $outdir
copyif $rundir/cluster$cnum-wordle.svg $outdir
done
echo "Done - Results are in $outdir"