-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassip
executable file
·247 lines (212 loc) · 6.45 KB
/
classip
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env bash
#
# classip :: ipv4 class information
# requires: bash 3.2+
trap 'echo -e "\nSIGINT Received... exiting"; exit 130' SIGINT
err() {
# send errors to standard error
echo "[${0##*/}] error: $*" >&2
}
show_help() {
# help option text
echo "Usage: ${0##*/} [OPTION]... [IP ADDRESS]..."
echo "Options:"
echo " -h, --help Show this help menu"
echo " -c Show all IP class ranges"
echo " -r Show all private IP ranges"
echo " -s Show all special IP ranges"
}
_parse_options() {
# parse arguments and assign variables, arrays
# check for long "--help"
if [[ "$1" == "--help" ]]; then
show_help
showed_help="1"
return 0
fi
# showed_help, class, private, special - must be declared in main
local opt=""
while getopts ":hcrs" opt; do
case "$opt" in
h)
show_help
showed_help="1"
return 0
;;
c) class="c" ;;
r) private="r" ;;
s) special="s" ;;
\?)
err "Invalid option: -$OPTARG"
show_help
return 1
;;
esac
done
}
print_class_info() {
# message echo'd to the terminal to see classes and ip ranges
echo " All IP Ranges"
echo "------------------------------------"
echo " Class Range"
echo "------------------------------------"
echo " A 0.0.0.0 - 127.255.255.255"
echo " B 128.0.0.0 - 191.255.255.255"
echo " C 192.0.0.0 - 223.168.255.255"
echo " D 224.0.0.0 - 239.255.255.255"
echo " E 240.0.0.0 - 255.255.255.255"
echo ""
}
print_private_info() {
# message echo'd to the terminal to see private ip ranges
echo " Private IP Ranges"
echo "--------------------------------------"
echo " Class Range"
echo "--------------------------------------"
echo " A 10.0.0.0 - 10.255.255.255"
echo " B 172.16.0.0 - 172.31.255.255"
echo " C 192.168.0.0 - 192.168.255.255"
echo " D 224.0.0.0 - 239.255.255.255"
echo " E 240.0.0.0 - 255.255.255.255"
echo ""
}
print_special_info() {
# message echo'd to the terminal to see special ip information
echo " Special IPv4 Addresses"
echo "---------------------------------------------------"
echo " Type Range"
echo "---------------------------------------------------"
echo " Loopback 127.0.0.1 - 127.255.255.255"
echo " Link-local / APIPA 169.254.0.0 - 169.254.255.255"
echo " Multicast (Class D) 224.0.0.0 - 239.255.255.255"
echo " Reserved (Class E) 240.0.0.0 - 255.255.255.255"
echo ""
}
option_printer() {
# print output based on arguments/options
#
# ARG $1: options string
# RET: none, runs print functions based on $1
[[ "$1" =~ c|r|s ]] && echo ""
[[ "$1" =~ c ]] && print_class_info
[[ "$1" =~ r ]] && print_private_info
[[ "$1" =~ s ]] && print_special_info
# return passed if no options were requested
return 0
}
validate_ip_address() {
# assess if a given ip address is a valid ipv4 address
#
# ARG $1: ip address
# RET: 1 if failed
if ! [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
err "\"${1}\" is not a valid IP address!"
return 1
fi
local -a ip_array=()
# convert ip address to an array
IFS='.' read -r -a ip_array <<< "$1"
local octet_value=""
for octet_value in "${ip_array[@]}"; do
if [[ "$octet_value" != "0" && "$octet_value" =~ ^0 ]]; then
err "\"${1}\" is not a valid IP address!"
return 1
fi
if (( octet_value < 0 || octet_value > 255 )); then
err "\"${1}\" is not a valid IP address!"
return 1
fi
done
# return - input validation passed
return 0
}
get_ip_class() {
# determine the ip class (A, B, C, D, E)
#
# ARG $1: ip address
# RET: ip class
local ip_first_octet="${1%%\.*}"
if (( ip_first_octet >= 0 && ip_first_octet <= 127 )); then
echo -n "A"
elif (( ip_first_octet >= 128 && ip_first_octet <= 191 )); then
echo -n "B"
elif (( ip_first_octet >= 192 && ip_first_octet <= 223 )); then
echo -n "C"
elif (( ip_first_octet >= 224 && ip_first_octet <= 239 )); then
echo -n "D"
elif (( ip_first_octet >= 240 && ip_first_octet <= 255 )); then
echo -n "E"
fi
}
get_ip_pripub() {
# determine if ip address is private or public
#
# ARG $1: ip address
# RET: private or public
local -a ip_array=()
IFS='.' read -r -a ip_array <<< "$1"
if (( ip_array[0] == 10 )); then
echo -n "private"
elif (( ip_array[0] == 169 && ip_array[1] == 254 )); then
echo -n "private"
elif (( ip_array[0] == 172 && ip_array[1] >= 16 && ip_array[1] <= 31 )); then
echo -n "private"
elif (( ip_array[0] == 192 && ip_array[1] == 168 )); then
echo -n "private"
elif (( ip_array[0] >= 224 && ip_array[0] <= 255 )); then
echo -n "private"
else
echo -n "public"
fi
}
get_ip_special() {
# determine if an ip address is a special address
# special addresses: loopback, link-local/apipa, multicast, reserved
#
# ARG $1: ip address
# RET: special ip type, or nothing
local -a ip_array=()
IFS='.' read -r -a ip_array <<< "$1"
if (( ip_array[0] == 127 )); then
echo -n "loopback"
elif (( ip_array[0] == 169 && ip_array[1] == 254 )); then
echo -n "link-local"
elif (( ip_array[0] >= 224 && ip_array[0] <= 239 )); then
echo -n "multicast"
elif (( ip_array[0] >= 240 && ip_array[0] <= 255 )); then
echo -n "reserved"
fi
}
main() {
set -eu
# define variables that can be assigned in _parse_options
local showed_help="" class="" private="" special=""
if [[ $# -gt 0 ]]; then
_parse_options "$@"
[[ -n "$showed_help" ]] && exit 0
else
err "One argument required: <option> and/or <ip address>"
echo "---"
show_help
exit 1
fi
shift $((OPTIND - 1)) # shift $@ past optional flags
option_printer "${class}${private}${special}"
local ip_address="" ip_class="" ip_pripub="" ip_special="" write_out=""
fail_rc="" # if any ip address was invalid; return non-zero rc
# iterate through ip addresses and gather and print information
for ip_address in "$@"; do
if ! validate_ip_address "$ip_address"; then
fail_rc="1"
continue
fi
ip_class=$(get_ip_class "$ip_address")
ip_pripub=$(get_ip_pripub "$ip_address")
ip_special=$(get_ip_special "$ip_address")
write_out="$ip_address : $ip_class $ip_pripub $ip_special"
echo "${write_out%"${write_out##*[![:space:]]}"}" # trim trailing whitespace
done
[[ -z "$fail_rc" ]] && exit 0
[[ -n "$fail_rc" ]] && exit 1
}
main "$@"