| 1 |
#!/bin/bash |
| 2 |
set -e |
| 3 |
|
| 4 |
# |
| 5 |
# input: massp25.og |
| 6 |
# |
| 7 |
# output: |
| 8 |
# massp25.og.ip.sort |
| 9 |
# 01-splitted/*.ptr |
| 10 |
# 01-splitted/*.weird |
| 11 |
# ptr.unique |
| 12 |
# |
| 13 |
|
| 14 |
#(( debug = 1 )) |
| 15 |
|
| 16 |
# requires $ip |
| 17 |
# echoes stdout and stderr e.g. |
| 18 |
# 217.92.231.121 mail.wessendorf.com iprev |
| 19 |
# 217.92.232.41 familie-nissen.eu noiprev |
| 20 |
# cannot use debug mode coz stdout gets redirected |
| 21 |
function checkiprev { |
| 22 |
[[ -z $ip ]] && echo function $0 requires \$ip && exit 1 |
| 23 |
|
| 24 |
# separate the commands to get the first return code |
| 25 |
ptrstr=`host -W3 $ip` || return |
| 26 |
#{ echo DEBUG: ip is $ip; return; } |
| 27 |
|
| 28 |
# we need to avoid backslash escaped foreign chars e.g. |
| 29 |
# \233\155\187\232\133\166\229\174\164003.ktnp.gov.tw. |
| 30 |
# we also don't want weird PTRs that would eventually validate iprev |
| 31 |
# ^R9d^O9^R8^A91^K8^R9^A89~-a.ktnp.gov.tw |
| 32 |
# *.highrockhosting7.com |
| 33 |
ptr=`echo $ptrstr | awk '{print $NF}' | grep -E '^[[:alnum:]._-]+$'` |
| 34 |
|
| 35 |
[[ -z $ptr ]] && echo weird Z ptr on ip $ip >> $piece.weird && return |
| 36 |
[[ $ptr = "" ]] && echo weird EMPTY ptr on ip $ip >> $piece.weird && return |
| 37 |
|
| 38 |
hostipstr=`host -W3 $ptr` |
| 39 |
#{ echo DEBUG: ptr is $ptr; return; } |
| 40 |
hostip=`echo $hostipstr | awk '{print $NF}'` |
| 41 |
|
| 42 |
if [[ $ip = $hostip ]]; then |
| 43 |
printf "$ip\t${ptr%\.}\tiprev\n" |
| 44 |
else |
| 45 |
printf "$ip\t${ptr%\.}\tnoiprev\n" |
| 46 |
fi |
| 47 |
} |
| 48 |
|
| 49 |
# requires $piece |
| 50 |
# writes stdout to $piece.ptr and stderr to terminal |
| 51 |
function processpiece { |
| 52 |
for ip in `cat $piece`; do |
| 53 |
if (( debug == 1 )); then |
| 54 |
checkiprev |
| 55 |
sleep 1 |
| 56 |
else |
| 57 |
checkiprev |
| 58 |
fi |
| 59 |
done > $piece.ptr && echo wrote to $piece.ptr || echo $piece.ptr FAIL; unset ip |
| 60 |
} |
| 61 |
|
| 62 |
[[ ! -f massp25.og ]] && echo missing file massp25.og && exit 1 |
| 63 |
|
| 64 |
# hmm masscan delivered duplicates |
| 65 |
# 11370506 massp25.og.ip |
| 66 |
# 11328069 massp25.og.ip.sort |
| 67 |
|
| 68 |
if [[ ! -f massp25.og.ip.sort && ! -s massp25.og.ip.sort ]]; then |
| 69 |
echo writing unique ip addresses to massp25.og.ip.sort |
| 70 |
time grep -v ^# massp25.og | awk '{print $4}' | sort --version-sort -u > massp25.og.ip.sort && echo done |
| 71 |
#sort -V -k4 |
| 72 |
fi |
| 73 |
wc -l massp25.og.ip.sort |
| 74 |
(( debug == 1 )) && echo everything fine? |
| 75 |
(( debug == 1 )) && read |
| 76 |
|
| 77 |
echo -n entering 01-splitted/ ... |
| 78 |
mkdir -p 01-splitted/ |
| 79 |
cd 01-splitted/ && echo done |
| 80 |
|
| 81 |
if [[ ! -f ip109 ]]; then |
| 82 |
echo -n splitting massp25.og.ip.sort into 110 pieces... |
| 83 |
split -a3 -d -nl/110 ../massp25.og.ip.sort ip && echo done |
| 84 |
fi |
| 85 |
wc -l ip109 |
| 86 |
(( debug == 1 )) && echo everything fine? |
| 87 |
(( debug == 1 )) && read |
| 88 |
|
| 89 |
echo cleaning-up previous attempt |
| 90 |
rm -f ip*.ptr |
| 91 |
rm -f ip*.weird |
| 92 |
|
| 93 |
echo |
| 94 |
echo $0 starts `date` |
| 95 |
echo |
| 96 |
|
| 97 |
if (( debug == 1 )); then |
| 98 |
echo DEBUG MODE - PROCESSING ONLY splitted/ip109 |
| 99 |
piece=ip109 |
| 100 |
processpiece & |
| 101 |
unset piece |
| 102 |
else |
| 103 |
for piece in ip[0-9][0-9][0-9]; do |
| 104 |
processpiece & |
| 105 |
done; unset piece |
| 106 |
#for x in `seq -w 000 050`; do |
| 107 |
# piece=ip$x |
| 108 |
# processpiece & |
| 109 |
#done; unset x |
| 110 |
fi |
| 111 |
|
| 112 |
echo -n "processes: " |
| 113 |
pgrep stage1 | wc -l |
| 114 |
|
| 115 |
cat <<EOF |
| 116 |
|
| 117 |
check with |
| 118 |
|
| 119 |
ps auxfww | grep stage1 | grep -v grep |
| 120 |
pgrep stage1 | wc -l |
| 121 |
tail -F 01-splitted/ip109.ptr |
| 122 |
|
| 123 |
EOF |
| 124 |
time wait |
| 125 |
|
| 126 |
# if this prints, we passed through without any fatal error (set -e is there) |
| 127 |
echo |
| 128 |
echo $0 ends `date` |
| 129 |
echo |
| 130 |
|
| 131 |
# all PTRs in one place, sorting and sanitizing |
| 132 |
echo -n writing to ptr.unique ... |
| 133 |
#sed 's/\.$//' |
| 134 |
#grep -E '^[[:alnum:]._-]+$' |
| 135 |
cut -f2 ../*.ptr | sort --version-sort -u > ptr.unique && echo done |
| 136 |
|
| 137 |
#TODO GETTING ALL IPREV ONLY |
| 138 |
|
| 139 |
#echo "writing to $iprev $noiprev $weird" |
| 140 |
#rm -f $iprev $noiprev $weird |
| 141 |
#iprev=massp25.iprev |
| 142 |
#noiprev=massp25.noiprev |
| 143 |
#weird=massp25.weird |
| 144 |
|