-
Notifications
You must be signed in to change notification settings - Fork 0
/
arin-inverse2prefix
executable file
·101 lines (94 loc) · 4.02 KB
/
arin-inverse2prefix
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
#!/bin/bash
#
# Copyright (c) 2022 Nagrind <https://github.com/nagrind>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
set -euo pipefail
shopt -s lastpipe
err() { echo "${@}" 1>&2; exit 1; }
usage() { err "Usage: $(basename "${0}") [-f] [-4|-6] <organization handle>"; }
declare mode=""
declare -i force=0
while getopts ":f46" opts; do
case "${opts}" in
f) force=1 ;;
4) # shellcheck disable=SC2015
[[ -z "${mode}" ]] && mode="NET" || err "Can not combine -4 with -6" ;;
6) # shellcheck disable=SC2015
[[ -z "${mode}" ]] && mode="NET6" || err "Can not combine -6 with -4" ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
[[ ${#} != 1 ]] && usage
# (Subset of) Searching Whois Using a CLI
#
# Query-by-record-type:
# To limit your query to a specific record type, include one of the following
# flags:
# n Network address space
# r CIDRized network space
# d Delegations
# a Autonomous systems
# p Points-of-contact
# o Organizations
# c End-user customers
# e Points-of-contact, Organizations, End-user customers
# z All of the above
#
# Query-by-attribute:
# To limit your query to a specific record attribute, include one of the
# following flags:
# @<domain name> Searches for matches by domain-portion of an
# email address
# ! <handle> Searches for matches by handle or id
# / <name> Searches for matches by name
# . <name> Searches for matches by name
# (same as above, but some WHOIS clients have
# problems with)
#
# Searches that retrieve a single record will display the full record.
# Searches that retrieve more than one record will be displayed in list
# output.
#
# Record hierarchy:
# Records in the ARIN WHOIS database have hierarchical relationships with
# other records. To display those related records, use the following flags:
#
# < Displays the record related up the hierarchy. For a network,
# display the supernet, or parent network in detailed format.
# > Displays the record related down the hierarchy. For a network,
# display the subdelegations, or subnets, below the network, in
# list format. For an organization or customer, display the
# resources registered to that organization or customer, in
# list format.
# = Display only an exact match in the hierarchy.
#
# Reference: https://www.arin.net/resources/registry/whois/rws/cli/
declare -i lines=0
whois -h whois.arin.net -- "o \! > ${1}" | \
# Handle ARIN WHOIS result limit somehow, see: "ACSP Suggestion 2012.20:
# Remove result limit for Whois-RWS child network queries", reference:
# https://www.arin.net/participate/community/acsp/suggestions/2012/2012-20/
{ result="$(tee)"; # Use tee(1), because /dev/stdin hangs on empty pipe
[[ "${result}" == *"query resulted in more than 256 records"* ]] && \
[[ ${force} == 0 ]] && err "Query resulted in more than 256 records" || \
echo "${result}"; } | \
gawk -F ') ' '/ \('"${mode:-NET6?}"'-/ { print $2 }' | \
while IFS=$'\n' read -r line; do
lines=$((++lines))
ipcalc --no-decorate --deaggregate="${line}"
done
# shellcheck disable=SC2015
[[ ${lines} == 0 ]] && err "No entries found" || :