/
getnets
executable file
·90 lines (86 loc) · 2.26 KB
/
getnets
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
#!/usr/bin/env bash
hash whois 2>/dev/null || { echo "whois is not found" &>2; DPND=1; }
hash sed 2>/dev/null || { echo "sed is not found" &>2; DPND=1; }
hash sort 2>/dev/null || { echo "sort is not found" &>2; DPND=1; }
if [[ "$DPND" = 1 ]]; then
echo &>2 "not all dependency met"
exit 1
fi
read -d '' help <<- EOF
getnets - script get all inetnums of autonomous system
usage: getnets -4 ASNUMBER for IPv4 subnets
getnets -6 ASNUMBER for IPv6 subnets
you can use --ipv4 or --ipv6 instead of short keys
you can NOT request both 4 & 6 at the same time, sadly...
EOF
if [[ ! $1 ]]; then
echo "$help"
exit 1
elif [[ $1 = '-h' || $1 = '--help' ]]; then
echo "$help"
exit 0
else
while :; do
case $1 in
-4|--ipv4)
if [[ $2 = '-6' ]]; then
echo "choose only v4 or v6, not both"
exit 1
elif [[ "$2" ]];then
AS4="$2"
shift 2
continue
else
echo >&2 "must specify AS number"
exit 1
fi
;;
-6|--ipv6)
if [[ "$2" ]];then
AS6="$2"
shift 2
continue
else
echo >&2 "must specify AS number"
exit 1
fi
;;
?*)
echo >&2 "unrecognized option $1"
;;
*)
break
esac
shift
done
fi
if [[ $AS4 && $AS6 ]]; then
echo "choose only v4 or v6, not both"
exit 1
elif [[ $AS4 ]]; then
AS=$AS4
IPV="g"
elif [[ $AS6 ]]; then
AS=$AS6
IPV="6"
fi
if [[ ! $AS ]]; then
exit 1
fi
if [[ $AS -lt 1 || $AS -eq 65535 || $AS -gt 4294967294 ]]; then
echo "invalid ASN"
exit 1
elif [[ $AS -gt 64511 && $AS -lt 65535 ]]; then
echo "private 16bit ASN"
exit 1
elif [[ $AS -gt 4200000000 && $AS -lt 4294967295 ]]; then
echo "private 32bit ASN"
exit 1
fi
WORD="as"
OUT=$(whois -h whois.radb.net "!$IPV$WORD$AS" | sed '1d' | sed '$d')
IFS=' ' read -ra NET <<< "$OUT"
SNET=($(printf "%s\n" "${NET[@]}" | sort -u))
for i in "${SNET[@]}"; do
echo "$i"
done