-
Notifications
You must be signed in to change notification settings - Fork 28
/
ship
1934 lines (1621 loc) · 68 KB
/
ship
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env bash
## title........: ship
## description..: a simple, handy network addressing multitool with plenty of features
## author.......: sotirios m. roussis a.k.a. xtonousou - xtonousou@gmail.com
## date.........: 20180612
## usage........: bash ship.sh [options]? [arguments]?
## bash version.: 3.2 or later
## license .....: gplv3+
### flags
if [[ "${COLOR}" -eq 1 ]]; then
declare -ra color_arr=(
$'\e[1;0m' # normal ## color_arr[0]
$'\e[1;31;40m' # red ## color_arr[1]
$'\e[1;32;40m' # green ## color_arr[2]
$'\e[1;33;40m' # orange ## color_arr[3]
$'\e[1;34;40m' # cyan ## color_arr[4]
$'\e[1;35;40m' # magenta ## color_arr[5]
)
elif [[ "${COLOR}" -eq 2 ]]; then
declare -ra color_arr=(
$'\e[1;0m' # normal ## color_arr[0]
$'\e[1;32;40m' # green ## color_arr[1]
$'\e[1;31;40m' # red ## color_arr[2]
$'\e[1;35;40m' # magenta ## color_arr[3]
$'\e[1;33;40m' # orange ## color_arr[4]
$'\e[1;34;40m' # cyan ## color_arr[5]
)
elif [[ "${COLOR}" -eq 3 ]]; then
declare -ra color_arr=(
$'\e[1;0m' # normal ## color_arr[0]
$'\e[1;34;40m' # cyan ## color_arr[1]
$'\e[1;35;40m' # magenta ## color_arr[2]
$'\e[1;32;40m' # green ## color_arr[3]
$'\e[1;31;40m' # red ## color_arr[4]
$'\e[1;33;40m' # orange ## color_arr[5]
)
elif [[ "${COLOR}" -eq 4 ]]; then
declare -ra color_arr=(
$'\e[1;0m' # normal ## color_arr[0]
$'\e[7;31;40m' # red ## color_arr[1]
$'\e[7;32;40m' # green ## color_arr[2]
$'\e[7;33;40m' # orange ## color_arr[3]
$'\e[7;34;40m' # cyan ## color_arr[4]
$'\e[7;35;40m' # magenta ## color_arr[5]
)
fi
[[ "${DEBUG}" -eq 1 ]] && set -x &> /dev/null
[[ "${SILENT}" -eq 1 ]] && readonly SILENT=1 &> /dev/null
[[ "${NOCHECK}" -eq 1 ]] && readonly NOCHECK=1 &> /dev/null
### script's info
readonly version="2.6.3"
readonly script_name="ship"
### author's info
readonly author="sotirios m. roussis"
readonly author_nickname="xtonousou"
readonly gmail="${author_nickname}@gmail.com"
readonly github="https://github.com/${author_nickname}"
### locations
readonly google="google.com"
readonly google_dns="8.8.8.8"
readonly mac_vendor="macvendors.co/api/vendorname"
declare -ra public_ip_hosts_arr=(
"ident.me"
"ipinfo.io/ip"
"wgetip.com"
"wtfismyip.com/text"
)
### timeouts
readonly short_timeout="2"
readonly timeout="6"
readonly long_timeout="17"
### dialogs
readonly dialog_under_development="under development"
readonly dialog_press_ctrlc="press [ctrl+c] to stop"
if [[ ! "${SILENT}" ]]; then
readonly dialog_error="try ${script_name} -h or ${script_name} --help for more information"
readonly dialog_aborting="aborting"
readonly dialog_invalid_bash="bash 3.2 or newer is required"
readonly dialog_no_arguments="no arguments"
readonly dialog_no_internet="internet connection unavailable"
readonly dialog_no_ipv6="ipv6 unavailable"
readonly dialog_no_ipv6_module="ipv6 is supported but the 'ipv6' module is not loaded"
readonly dialog_no_local_connection="local connection unavailable"
readonly dialog_destination_unreachable="destination is unreachable"
readonly dialog_server_is_down="destination is unreachable. server may be down or has connection issues"
readonly dialog_no_valid_mask="the netmask is invalid"
readonly dialog_no_valid_cidr="the cidr is invalid"
readonly dialog_no_valid_addresses="no valid ipv4, ipv6 or mac addresses found"
readonly dialog_no_valid_mac="no valid mac address/es found"
readonly dialog_no_tracepath6="tracepath6 is missing, will use tracepath with no ipv6 compatibility"
readonly dialog_no_trace_command="you must install at least one of the following tools to perform this action: tracepath, traceroute, mtr"
readonly dialog_root_permissions="${script_name} requires root privileges for this action"
fi
########################################################################
# #
# helpful functions to print or check, verify and test various things #
# #
########################################################################
# initializes a set of regexps variables (ipv4, ipv6, with and without cidr).
init_regexes() {
# mac
regex_mac="([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}"
# ipv4
regex_ipv4="((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|"
regex_ipv4+="(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"
# ipv4 with cidr notation
regex_ipv4_cidr="(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|"
regex_ipv4_cidr+="25[0-5])\\.){3}([0-9]|""[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|"
regex_ipv4_cidr+="25[0-5])(\\/([0-9]|[1-2][0-9]|3[0-2]))"
# ipv6
regex_ipv6="([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|"
regex_ipv6+="([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:"
regex_ipv6+="[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4})"
regex_ipv6+="{1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|"
regex_ipv6+="([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]"
regex_ipv6+="{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:"
regex_ipv6+="[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|"
regex_ipv6+="fe08:(:[0-9a-fA-F]{1,4}){2,2}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4})"
regex_ipv6+="{0,1}:){0,1}${regex_ipv4}|([0-9a-fA-F]{1,4}:){1,4}:${regex_ipv4}"
# ipv6 with cidr notation
regex_ipv6_cidr="^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|"
regex_ipv6_cidr+="(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|"
regex_ipv6_cidr+="2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|"
regex_ipv6_cidr+=":))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|"
regex_ipv6_cidr+=":((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|"
regex_ipv6_cidr+="[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]"
regex_ipv6_cidr+="{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|"
regex_ipv6_cidr+="[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|"
regex_ipv6_cidr+="(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|"
regex_ipv6_cidr+="((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)"
regex_ipv6_cidr+="(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]"
regex_ipv6_cidr+="{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4})"
regex_ipv6_cidr+="{0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|"
regex_ipv6_cidr+="[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]"
regex_ipv6_cidr+="{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|"
regex_ipv6_cidr+="1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|"
regex_ipv6_cidr+="(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}"
regex_ipv6_cidr+=":((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|"
regex_ipv6_cidr+="[1-9]?d)){3}))|:)))(%.+)?s*(\\/([0-9]|[1-9][0-9]|"
regex_ipv6_cidr+="1[0-1][0-9]|12[0-8]))?$"
return 0
}
# convert a decimal to binary (0-255).
dec_to_bin() {
local d2b
d2b=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
printf "%s" "${d2b[${1}]}"
return 0
}
# convert a decimal to a hexadecimal.
dec_to_hex() {
printf "%#X" "${1}"
return 0
}
# convert binary to a decimal.
bin_to_dec() {
printf "%s" "$((2#${1}))"
return 0
}
# returns the integer representation of an ip arg,
# passed in ascii dotted-decimal notation (x.x.x.x).
dotted_quad_ip_to_decimal() {
local a b c d
IFS=. read -r a b c d <<< "${1}"
printf '%d\n' "$(( a * 256 ** 3 + b * 256 ** 2 + c * 256 + d ))"
return 0
}
# prints a message while checking a network host.
print_check() {
[[ ! "${SILENT}" ]] \
&& printf "checking ${color_arr[2]}%s${color_arr[0]} ..." "${1}"
return 0
}
# clears previous line.
clear_line() {
printf "\\r\\033[K"
return 0
}
# prints a list of most common ports with protocols.
print_port_protocol_list() {
local item
declare -ra ports_array=(
"20-21" "22" "23" "25" "53" "67-68" "69" "80" "110" "123" "137-139" "143"
"161-162" "179" "389" "443" "636" "989-990"
)
declare -ra ports_tcp_udp_array=(
"tcp" "tcp" "tcp" "tcp" "tcp/udp" "udp" "udp" "tcp" "tcp" "udp" "tcp/udp"
"tcp" "tcp/udp" "tcp" "tcp/udp" "tcp" "tcp/udp" "tcp"
)
declare -ra ports_protocol_array=(
"ftp" "ssh" "telnet" "smtp" "dns" "dhcp" "tftp" "http" "popv3" "ntp"
"netbios" "imap" "snmp" "bgp" "ldap" "https" "ldaps" "ftp over tls/ssl"
)
for item in "${!ports_array[@]}"; do
printf "%-17s%-8s%s\\n" "${ports_protocol_array[item]}" "${ports_tcp_udp_array[item]}" "${ports_array[item]}"
done
return 0
}
# used with zero parameters: exit 1.
# used with one parameter : echoes parameter, usually error dialogs.
# used with two parameters : invalid option, then echoes first parameter, usually error dialogs.
error_exit() {
[[ ! "${SILENT}" ]] \
&& [[ -z "${1}" ]] && clear_line && exit 1 \
|| [[ -z "${2}" ]] \
&& clear_line \
&& printf "%s\\n" "${1}" \
&& exit 1 \
|| clear_line \
&& printf "%s\\n" "${script_name}: invalid option '${2}'" \
&& printf "%s\\n" "${1}" && \
exit 1 \
|| exit 1
}
# exits ship, if ping fails to reach $1 in an amount of time.
check_destination() {
local clean_destination returned_value
clean_destination=$(printf "%s" "${1}" | sed 's/^http\(\|s\):\/\///g' | cut --fields=1 --delimiter="/")
timeout "${long_timeout}" ping -q -c 1 "${clean_destination}" &>/dev/null \
|| returned_value="${?}"
[[ "${returned_value}" -ge 2 ]] && error_exit "${dialog_destination_unreachable}"
return 0
}
check_http_code() {
local http_code
http_code=$(curl --location --output /dev/null --silent --head --write-out '%{http_code}\n' "${1}")
return "${http_code}"
}
# checks network connection (local or internet).
check_connectivity() {
case "${1}" in
"--local")
ip route | grep ^default &>/dev/null \
|| error_exit "${dialog_no_local_connection}"
;;
"--internet")
[[ $(check_http_code "google.com"; echo "${?}") = 200 ]] \
|| error_exit "${dialog_no_internet}"
;;
esac
return 0
}
# checks if a network address is valid.
check_dotted_quad_address() {
local decimal_points IFS part_a part_b part_c part_d
decimal_points=$(printf "%s" "${1}" | grep --only-matching "\\." | wc --lines)
# check if there are three dots
[[ "${decimal_points}" -ne 3 ]] && show_usage_ipcalc && error_exit
IFS=.
read -r part_a part_b part_c part_d <<< "${1}"
# check for non numerical values
[[ ! "${part_a}" =~ ^[0-9]+$ || ! "${part_b}" =~ ^[0-9]+$ || ! "${part_c}" =~ ^[0-9]+$ || ! "${part_d}" =~ ^[0-9]+$ ]] \
&& show_usage_ipcalc \
&& error_exit
# check if any part is empty
[[ ! "${part_a}" || ! "${part_b}" || ! "${part_c}" || ! "${part_d}" ]] \
&& show_usage_ipcalc \
&& error_exit
# check if any part of the address is < 0 or > 255
[[ "${part_a}" -lt 0 || "${part_a}" -gt 255 || "${part_b}" -lt 0 || "${part_b}" -gt 255 || "${part_c}" -lt 0 || "${part_c}" -gt 255 || "${part_d}" -lt 0 || "${part_d}" -gt 255 ]] \
&& show_usage_ipcalc \
&& error_exit
IFS=
return 0
}
# checks if ipv6 is available, if not exit.
check_ipv6() {
grep -i "ipv6" "/proc/modules" &> /dev/null \
|| printf "%s\\n" "${dialog_no_ipv6_module}"
[[ -f "/proc/net/if_inet6" ]] \
|| error_exit "${dialog_no_ipv6}"
return 0
}
# checks if an argument is passed, if not exit.
# $1=error message, $2=argument
check_for_missing_args() {
[[ -z "${2}" ]] && error_exit "${1}"
return 0
}
# numerical verification.
check_if_parameter_is_positive_integer() {
[[ ! "${1}" =~ ^[0-9]+$ ]] \
&& error_exit "${1} is not a positive integer. ${dialog_aborting}" "${1}"
return 0
}
# checks for root privileges.
check_root_permissions() {
[[ "$(id -u)" -ne 0 ]] \
&& error_exit "${dialog_root_permissions}"
return 0
}
# checks bash version. minimum is version 3.2.
check_bash_version() {
[[ "${BASH_VERSINFO[0]}${BASH_VERSINFO[1]}" -lt 32 ]] \
&& error_exit "${dialog_invalid_bash}"
return 0
}
# deletes every file that is created by this script. usually in /tmp.
mr_proper() {
rm --recursive --force "/tmp/${script_name}"*
return 0
}
# background tasks' handler.
handle_jobs() {
local job
for job in $(jobs -p); do
wait "${job}"
done
return 0
}
# traps int and sigtstp.
trap_handler() {
local yesno
printf "\\n"
while [[ ! "${yesno}" =~ ^[YyNn]$ ]]; do
printf "%s " "exit? [y/n]"
read -r yesno &>/dev/null
done
[[ "${yesno}" = "N" ]] && yesno="n"
[[ "${yesno}" = "Y" ]] && yesno="y"
[[ "${yesno}" = "y" ]] && handle_jobs && exit 0
return 0
}
########################################################################
# #
# main script's functions in alphabetical order based on show_usage() #
# #
########################################################################
# prints active network interfaces with their ipv4 address.
show_ipv4() {
local item
declare -a ipv4_array
declare -ar interfaces_array=($(ip route | awk 'tolower($0) ~ /default/ {print $5}'))
for item in "${!interfaces_array[@]}"; do
ipv4_array[item]=$(ip -4 address show dev "${interfaces_array[item]}" | awk -v family=inet '$0 ~ family {print $2}' | cut --delimiter="/" --fields=1)
printf "%s %s\\n" "${interfaces_array[item]}" "${ipv4_array[item]}"
done
return 0
}
# prints active network interfaces with their ipv6 address.
show_ipv6() {
[[ ! "${NOCHECK}" ]] && check_ipv6
local item
declare -a ipv6_array
declare -ar interfaces_array=($(ip route | awk 'tolower($0) ~ /default/ {print $5}'))
for item in "${!interfaces_array[@]}"; do
ipv6_array[item]=$(ip -6 address show dev "${interfaces_array[item]}" | awk -v family="inet6" 'tolower($0) ~ family {print $2}' | cut --delimiter="/" --fields=1)
printf "%s %s\\n" "${interfaces_array[item]}" "${ipv6_array[item]}"
done
return 0
}
# prints all "basic" info.
show_all() {
[[ ! "${NOCHECK}" ]] && check_ipv6
local mac_of driver_of gateway item
declare -a ipv4_array ipv6_array
declare -ar interfaces_array=($(ip route | awk 'tolower($0) ~ /default/ {print $5}'))
for item in "${!interfaces_array[@]}"; do
ipv4_array[item]=$(ip -4 address show dev "${interfaces_array[item]}" | awk -v family=inet '$0 ~ family {print $2}' | cut --delimiter="/" --fields=1)
ipv6_array[item]=$(ip -6 address show dev "${interfaces_array[item]}" | awk -v family="inet6" 'tolower($0) ~ family {print $2}' | cut --delimiter="/" --fields=1)
[[ -f "/sys/class/net/${interfaces_array[item]}/phy80211/device/uevent" ]] \
&& driver_of=$(awk -F '=' 'tolower($0) ~ /driver/{print $2}' "/sys/class/net/${interfaces_array[item]}/phy80211/device/uevent") \
|| driver_of=$(awk -F '=' 'tolower($0) ~ /driver/{print $2}' "/sys/class/net/${interfaces_array[item]}/device/uevent")
mac_of=$(awk '{print $0}' "/sys/class/net/${interfaces_array[item]}/address" 2> /dev/null)
gateway=$(ip route | awk "/${interfaces_array[item]}/ && tolower(\$0) ~ /default/ {print \$3}")
printf "%s %s %s %s %s %s\\n" "${interfaces_array[item]}" "${driver_of}" "${mac_of}" "${gateway}" "${ipv4_array[item]}" "${ipv6_array[item]}"
done
return 0
}
# prints all available network interfaces.
show_all_interfaces() {
ip link show | \
awk '/^[0-9]/{printf "%s ", $2}' | sed 's/://g' | sed 's/ *$//g'
printf "\\n"
}
# prints the driver used of active interface.
show_driver() {
local driver_of item
declare -ar interfaces_array=($(ip route | awk 'tolower($0) ~ /default/ {print $5}'))
for item in "${!interfaces_array[@]}"; do
[[ -f "/sys/class/net/${interfaces_array[item]}/phy80211/device/uevent" ]] \
&& driver_of=$(awk -F '=' 'tolower($0) ~ /driver/{print $2}' "/sys/class/net/${interfaces_array[item]}/phy80211/device/uevent") \
|| driver_of=$(awk -F '=' 'tolower($0) ~ /driver/{print $2}' "/sys/class/net/${interfaces_array[item]}/device/uevent")
printf "%s %s\\n" "${interfaces_array[item]}" "${driver_of}"
done
return 0
}
# prints the external ip address/es. if $1 is empty prints user's public ip, if not, $1 should be like example.com. otherwise, if more arguments are passed, it prints their external ips with their domain name at right.
show_ip_from() {
local http_code temp_file random_source input item host
random_source="${public_ip_hosts_arr["$(( RANDOM % ${#public_ip_hosts_arr[@]} ))"]}"
temp_file="/tmp/${script_name}.ips"
touch "${temp_file}"
if [[ -z "${1}" ]]; then
[[ ! "${SILENT}" ]] && print_check "${random_source}"
[[ ! "${NOCHECK}" ]] \
&& check_destination "${random_source}"
clear_line
[[ ! "${SILENT}" ]] && printf "%s" "grabbing ip ..."
curl "${random_source}" --silent --output "${temp_file}"
clear_line
awk '{print $0}' "${temp_file}"
elif [[ "${#}" -eq 1 ]]; then
[[ ! "${SILENT}" ]] && print_check "${1}"
[[ ! "${NOCHECK}" ]] && check_destination "${1}"
clear_line
input=$(printf "%s" "${1}" | sed --expression='s/^http\(\|s\):\/\///g' --expression='s/^`//' --expression='s/`//' --expression='s/`$//' | cut --fields=1 --delimiter="/")
ping_source() {
for item in {1..10}; do
ping -c 1 -w "${long_timeout}" "${input}" 2> /dev/null | awk -F '[()]' '/PING/{print $2}' >> "${temp_file}" &
done
handle_jobs
}
[[ ! "${SILENT}" ]] && printf "%s ${color_arr[2]}%s${color_arr[0]} %s" "pinging" "${1}" "..."
ping_source
clear_line
sort --version-sort --unique "${temp_file}"
elif [[ "${#}" -gt 1 ]]; then
for host in "${@}"; do
[[ ! "${SILENT}" ]] && print_check "${host}"
[[ ! "${NOCHECK}" ]] && check_destination "${host}"
clear_line
input=$(printf "%s" "${host}" | sed --expression='s/^http\(\|s\):\/\///g' --expression='s/^`//' --expression='s/`//' --expression='s/`$//' | cut --fields=1 --delimiter="/")
[[ ! "${SILENT}" ]] && printf "%s ${color_arr[2]}%s${color_arr[0]} %s" "pinging" "${input}" "..."
for item in {1..5}; do
( printf "%s %s\\n" "$(ping -c 1 -w "${long_timeout}" "${input}" 2> /dev/null | awk -F '[()]' '/PING/{print $2}')" "${input}" &>> "${temp_file}" ) &
done
clear_line
handle_jobs
sort --version-sort --unique "${temp_file}"
done
fi
return 0
}
# prints all valid ipv4, ipv6 and mac addresses extracted from file.
show_ips_from_file() {
local file
[[ -z "${1}" ]] && error_exit "no file was specified. ${dialog_aborting}"
for file in "${@}"; do
[[ ! -f "${file}" ]] && error_exit "${color_arr[3]}${file}${color_arr[0]} does not exist. ${dialog_aborting}"
done
local temp_file_ipv4 temp_file_ipv6 temp_file_mac
local is_temp_file_ipv4_empty is_temp_file_ipv6_empty is_temp_file_mac_empty
temp_file_ipv4="/tmp/${script_name}.ipv4"
temp_file_ipv6="/tmp/${script_name}.ipv6"
temp_file_mac="/tmp/${script_name}.mac"
touch "${temp_file_ipv4}"
touch "${temp_file_ipv6}"
touch "${temp_file_mac}"
init_regexes
for file in "${@}"; do
grep --extended-regexp --only-matching "${regex_ipv4}" "${file}" 2>/dev/null >> "${temp_file_ipv4}"
grep --extended-regexp --only-matching "${regex_ipv6}" "${file}" 2>/dev/null >> "${temp_file_ipv6}"
grep --extended-regexp --only-matching "${regex_mac}" "${file}" 2>/dev/null >> "${temp_file_mac}"
done
sort --version-sort --unique --output="${temp_file_ipv4}" "${temp_file_ipv4}"
sort --version-sort --unique --output="${temp_file_ipv6}" "${temp_file_ipv6}"
sort --version-sort --unique --output="${temp_file_mac}" "${temp_file_mac}"
[[ -s "${temp_file_ipv4}" ]] && is_temp_file_ipv4_empty=0 || is_temp_file_ipv4_empty=1
[[ -s "${temp_file_ipv6}" ]] && is_temp_file_ipv6_empty=0 || is_temp_file_ipv6_empty=1
[[ -s "${temp_file_mac}" ]] && is_temp_file_mac_empty=0 || is_temp_file_mac_empty=1
case "${is_temp_file_ipv4_empty}:${is_temp_file_ipv6_empty}:${is_temp_file_mac_empty}" in
0:0:0) # ipv4, ipv6 and mac addresses
paste "${temp_file_ipv4}" "${temp_file_ipv6}" "${temp_file_mac}" | \
awk -F '\t' '{printf("%-15s │ %-39s │ %s\n", $1, tolower($2), tolower($3))}'
;;
0:0:1) # only ipv4 and ipv6 addresses
paste "${temp_file_ipv4}" "${temp_file_ipv6}" | \
awk -F '\t' '{printf("%-15s │ %s\n", $1, tolower($2))}'
;;
0:1:0) # only ipv4 and mac addresses
paste "${temp_file_ipv4}" "${temp_file_mac}" | \
awk -F '\t' '{printf("%-15s │ %s\n", $1, tolower($2))}'
;;
0:1:1) # only ipv4 addresses
paste "${temp_file_ipv4}" | \
awk -F '\t' '{printf("%s\n", $1)}'
;;
1:0:0) # only ipv6 and mac addresses
paste "${temp_file_ipv6}" "${temp_file_mac}" | \
awk -F '\t' '{printf("%-39s │ %s\n", tolower($1), tolower($2))}'
;;
1:0:1) # only ipv6 addresses
paste "${temp_file_ipv6}" | \
awk -F '\t' '{printf("%s\n", tolower($1))}'
;;
1:1:0) # only mac addresses
paste "${temp_file_mac}" | \
awk -F '\t' '{printf("%s\n", tolower($1))}'
;;
1:1:1) # none
error_exit "${dialog_no_valid_addresses}"
;;
esac
return 0
}
# prints active network interfaces and their gateway.
show_gateway() {
local gateway item
declare -ar interfaces_array=($(ip route | awk 'tolower($0) ~ /default/ {print $5}'))
for item in "${!interfaces_array[@]}"; do
gateway=$(ip route | awk "/${interfaces_array[item]}/ && tolower(\$0) ~ /default/ {print \$3}")
printf "%s %s\\n" "${interfaces_array[item]}" "${gateway}"
done
return 0
}
# scans live hosts on network and prints their ipv4 address with or without mac address. icmp and arp.
show_live_hosts() {
[[ ! "${NOCHECK}" ]] && check_root_permissions
local online_interface network_ip network_ip_cidr filtered_ip host
online_interface=$(ip route get "${google_dns}" | awk -F 'dev ' 'NR == 1 {split($2, a, " "); print a[1]}')
network_ip=$(ip route | awk "/${online_interface}/ && /src/ {print \$1}" | cut --fields=1 --delimiter="/")
network_ip_cidr=$(ip route | awk "/${online_interface}/ && /src/ {print \$1}")
filtered_ip=$(printf "%s" "${network_ip}" | awk 'BEGIN{FS=OFS="."} NF--')
ip -statistics neighbour flush all &>/dev/null
[[ ! "${SILENT}" ]] && printf "%s ${color_arr[2]}%s${color_arr[0]}, %s %s" "pinging" "${network_ip_cidr}" "please wait" "..."
for host in {1..254}; do
ping "${filtered_ip}.${host}" -c 1 -w "${long_timeout}" &>/dev/null &
done
handle_jobs
clear_line
init_regexes
case "${1}" in
"--normal")
ip neighbour | \
awk 'tolower($0) ~ /reachable|stale|delay|probe/{print $1}' | \
sort --version-sort --unique
;;
"--mac")
ip neighbour | \
awk 'tolower($0) ~ /reachable|stale|delay|probe/{printf ("%5s\t%s\n", $1, $5)}' | \
sort --version-sort --unique
;;
esac
return 0
}
# prints active network interfaces.
show_interfaces() {
declare -ar interfaces_array=($(ip route | awk 'tolower($0) ~ /default/ {print $5}'))
printf "%s\\n" "${interfaces_array[@]}"
return 0
}
# prints a list of private and reserved ips. $1 "normal" or "cidr".
show_bogon_ips() {
local ip
declare -ar ipv4_bogon_array=(
"0.0.0.0" "10.0.0.0" "100.64.0.0" "127.0.0.0" "127.0.53.53" "169.254.0.0"
"172.16.0.0" "192.0.0.0" "192.0.2.0" "192.168.0.0" "198.18.0.0"
"198.51.100.0" "203.0.113.0" "224.0.0.0" "240.0.0.0" "255.255.255.255"
)
declare -ar ipv6_bogon_array=(
"::" "::1" "::ffff:0:0" "::" "100::" "2001:10::" "2001:db8::" "fc00::"
"fe80::" "fec0::" "ff00::"
)
declare -ar ipv4_cidr_bogon_array=(
"0.0.0.0/8" "10.0.0.0/8" "100.64.0.0/10" "127.0.0.0/8" "127.0.53.53/8"
"169.254.0.0/16" "172.16.0.0/12" "192.0.0.0/24" "192.0.2.0/24" "192.168.0.0/16"
"198.18.0.0/15" "198.51.100.0/24" "203.0.113.0/24" "224.0.0.0/4" "240.0.0.0/4"
"255.255.255.255/32"
)
declare -ar ipv6_cidr_bogon_array=(
"::/128" "::1/128" "::ffff:0:0/96" "::/96" "100::/64" "2001:10::/28"
"2001:db8::/32" "fc00::/7" "fe80::/10" "fec0::/10" "ff00::/8"
)
declare -ar ipv4_dialog_array=(
"'this' network" "private-use networks" "carrier-grade nat" "loopback"
"name collision occurrence" "link local" "private-use networks"
"ietf protocol assignments" "test-net-1" "private-use networks"
"network interconnect device benchmark testing" "test-net-2" "test-net-3"
"multicast" "reserved for future use" "limited broadcast"
)
declare -ar ipv6_dialog_array=(
"node-scope unicast unspecified address" "node-scope unicast loopback address"
"ipv4-mapped addresses" "ipv4-compatible addresses"
"remotely triggered black hole addresses"
"overlay routable cryptographic hash identifiers (orchid)"
"documentation prefix" "unique local addresses (ula)" "link-local unicast"
"site-local unicast (deprecated)"
"multicast (note: ff0e:/16 is global scope and may appear on the global internet)"
)
case "${1}" in
"--normal")
for ip in "${!ipv4_dialog_array[@]}"; do
printf "%-16s%s\\n" "${ipv4_bogon_array[ip]}" "${ipv4_dialog_array[ip]}"
done
for ip in "${!ipv6_dialog_array[@]}"; do
printf "%-16s%s\\n" "${ipv6_bogon_array[ip]}" "${ipv6_dialog_array[ip]}"
done
;;
"--cidr")
for ip in "${!ipv4_dialog_array[@]}"; do
printf "%-19s%s\\n" "${ipv4_cidr_bogon_array[ip]}" "${ipv4_dialog_array[ip]}"
done
for ip in "${!ipv6_dialog_array[@]}"; do
printf "%-19s%s\\n" "${ipv6_cidr_bogon_array[ip]}" "${ipv6_dialog_array[ip]}"
done
;;
esac
return 0
}
# prints active network interfaces with their mac address.
show_mac() {
local mac_of item
declare -ar interfaces_array=($(ip route | awk 'tolower($0) ~ /default/ {print $5}'))
for item in "${!interfaces_array[@]}"; do
mac_of=$(awk '{print $0}' "/sys/class/net/${interfaces_array[item]}/address" 2> /dev/null)
printf "%s %s\\n" "${interfaces_array[item]}" "${mac_of}"
done
return 0
}
# shows neighbor table.
show_neighbor_cache() {
local temp_file
temp_file="/tmp/${script_name}.file"
touch "${temp_file}"
ip neigh \
| awk 'tolower($0) ~ /permanent|noarp|stale|reachable|router|incomplete|delay|probe/{printf ("%-16s%-20s%s\n", $1, $5, $6)}' >> "${temp_file}"
awk '{print $0}' "${temp_file}" | sort --version-sort
return 0
}
# prints connections and the count of them per ip.
show_port_connections() {
[[ -z "${1}" ]] && print_port_protocol_list && exit 0
[[ ! "${NOCHECK}" ]] && check_root_permissions
[[ ! "${NOCHECK}" ]] && check_if_parameter_is_positive_integer "${1}"
init_regexes
clear
while :; do
clear
if [[ ! "${SILENT}" ]]; then
printf "%s\\n\\n" "${dialog_press_ctrlc}"
printf " ${color_arr[2]}%s${color_arr[1]} %s %s ${color_arr[2]}%s\\n" "┌─>" "count" "port" "──┐"
printf " %s %s ${color_arr[1]}%s ${color_arr[2]}%s ${color_arr[1]}%s\\n" "│" "┌───────>" "ipv4" "└─>" "${1}"
printf " ${color_arr[2]}%s %s%s${color_arr[0]}\\n" "┌─┘" "└──────────────┐"
fi
ss --all --numeric --process | grep --extended-regexp "${regex_ipv4}" | grep ":${1}" | awk '{print $6}' | cut --delimiter=":" --fields=1 | sort --version-sort | uniq --count
sleep 2
done
return 0
}
# prints hops to a destination. $1=--ipv4|--ipv6, $2=network destination.
show_next_hops() {
local filtered_input protocol tracepath_cmd traceroute_cmd mtr_cmd temp_file
temp_file="/tmp/${script_name}.file"
touch "${temp_file}"
hash tracepath &>/dev/null && tracepath_cmd=1 || tracepath_cmd=0
hash traceroute &>/dev/null && traceroute_cmd=1 || traceroute_cmd=0
hash mtr &>/dev/null && mtr_cmd=1 || mtr_cmd=0
filtered_input=$(echo "${2}" | sed 's/^http\(\|s\):\/\///g' | cut --fields=1 --delimiter="/")
[[ ! "${NOCHECK}" ]] && check_for_missing_args "${dialog_no_arguments}" "${filtered_input}"
case "${1}" in
"--ipv4")
protocol=4
;;
"--ipv6")
check_ipv6
protocol=6
;;
esac
print_check "${filtered_input}"
[[ ! "${NOCHECK}" ]] && check_destination "${filtered_input}"
init_regexes
trace_hops() {
# traceroute is deprecated, nevertheless it is preferred over all
case "${tracepath_cmd}:${traceroute_cmd}:${mtr_cmd}" in
# if none of the tools (tracepath, traceroute, mtr) is installed
0:0:0)
echo -e "${dialog_no_trace_command}"
;;
# if it is installed 'mtr' only
0:0:1)
case "${protocol}" in
4)
mtr -"${protocol}" --report-cycles 2 --no-dns --report "${filtered_input}" 2> /dev/null | \
grep --extended-regexp --only-matching "${regex_ipv4}"
;;
6)
mtr -"${protocol}" --report-cycles 2 --no-dns --report "${filtered_input}" 2> /dev/null | \
grep --extended-regexp --only-matching "${regex_ipv6}"
;;
esac
;;
# if it is installed 'traceroute' only
0:1:0)
case "${protocol}" in
4)
timeout "${short_timeout}" traceroute -"${protocol}" -n "${filtered_input}" 2> /dev/null | \
tail --lines=+2 | \
grep --extended-regexp --only-matching "${regex_ipv4}"
;;
6)
timeout "${short_timeout}" traceroute -"${protocol}" -n "${filtered_input}" 2> /dev/null | \
tail --lines=+2 | \
grep --extended-regexp --only-matching "${regex_ipv6}"
;;
esac
;;
# if it is installed 'traceroute' and 'mtr' only
0:1:1)
case "${protocol}" in
4)
mtr -"${protocol}" --report-cycles 2 --no-dns --report "${filtered_input}" 2> /dev/null | \
grep --extended-regexp --only-matching "${regex_ipv4}"
;;
6)
mtr -"${protocol}" --report-cycles 2 --no-dns --report "${filtered_input}" 2> /dev/null | \
grep --extended-regexp --only-matching "${regex_ipv6}"
;;
esac
;;
# if it is installed 'tracepath' only
1:0:0)
# tracepath6 workaround: many linux distributions do not have tracepath6 (it is included in manpages tho :/)
hash tracepath6 &>/dev/null && protocol=6
[[ "${protocol}" -eq 4 ]] && echo -e "${dialog_no_tracepath6}"
case "${protocol}" in
4)
timeout "${short_timeout}" tracepath"${protocol}" -n "${filtered_input}" 2> /dev/null | \
awk '{print $2}' | \
grep --extended-regexp --only-matching "${regex_ipv4}"
;;
6)
timeout "${short_timeout}" tracepath"${protocol}" -n "${filtered_input}" 2> /dev/null | \
awk '{print $2}' | \
grep --extended-regexp --only-matching "${regex_ipv6}"
;;
esac
;;
# if it is installed 'tracepath' and 'mtr' only
1:0:1)
case "${protocol}" in
4)
mtr -"${protocol}" --report-cycles 2 --no-dns --report "${filtered_input}" 2> /dev/null | \
grep --extended-regexp --only-matching "${regex_ipv4}"
;;
6)
mtr -"${protocol}" --report-cycles 2 --no-dns --report "${filtered_input}" 2> /dev/null | \
grep --extended-regexp --only-matching "${regex_ipv6}"
;;
esac
;;
# if it is installed 'tracepath' and 'traceroute' only
1:1:0)
case "${protocol}" in
4)
timeout "${short_timeout}" traceroute -"${protocol}" -n "${filtered_input}" 2> /dev/null | \
tail --lines=+2 | \
grep --extended-regexp --only-matching "${regex_ipv4}"
;;
6)
timeout "${short_timeout}" traceroute -"${protocol}" -n "${filtered_input}" 2> /dev/null | \
tail --lines=+2 | \
grep --extended-regexp --only-matching "${regex_ipv6}"
;;
esac
;;
# if it is installed 'tracepath', 'traceroute' and 'mtr'
1:1:1)
case "${protocol}" in
4)
timeout "${short_timeout}" traceroute -"${protocol}" -n "${filtered_input}" 2> /dev/null | \
tail --lines=+2 | \
grep --extended-regexp --only-matching "${regex_ipv4}"
;;
6)
timeout "${short_timeout}" traceroute -"${protocol}" -n "${filtered_input}" 2> /dev/null | \
tail --lines=+2 | \
grep --extended-regexp --only-matching "${regex_ipv6}"
;;
esac
;;
esac >> "${temp_file}"
}
[[ ! "${SILENT}" ]] \
&& clear_line \
&& echo -ne "tracing path to ${color_arr[2]}${filtered_input}${color_arr[0]} ..."
trace_hops
# ensure that temp_file is written on /tmp
while [[ ! -f "${temp_file}" ]]; do
trace_hops
done
clear_line
awk '!seen[$0]++ {print}' "${temp_file}"
return 0
}
# shows broadcast, network address, cisco wildcard mask, class and host range by given ipv4 address and netmask.
show_ipcalc() {
local nobinary html cidr decimal_points bits
local hosts host_minimum host_minimum_binary host_maximum host_maximum_binary
local class class_description ip ip_binary netmask netmask_binary
local wildcard wildcard_binary network_address network_address_binary
local broadcast_address broadcast_address_binary
local ip_part_a ip_part_b ip_part_c ip_part_d
local netmask_part_a netmask_part_b netmask_part_c netmask_part_d