-
Notifications
You must be signed in to change notification settings - Fork 14
/
fixetchosts.sh
60 lines (44 loc) · 1.51 KB
/
fixetchosts.sh
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
#!/bin/bash
# 2022 Dave Bechtel
# Utility: Prints ONLY the lines that need to be fixed in a malformed /etc/hosts
# Canonical format: IP FQDN shortname(s)
# Fix malformed line(s): IP short FQDN
# Handles a limited number of extra aliases (up to 5)
# Does NOT modify original /etc/hosts and MAY NOT handle IPv6 (not tested)
infile=/etc/hosts
#infile=/tmp/hosts.test
outfile=/tmp/hosts.fixed
>$outfile #clearit
declare -i checkfq # integer
#set -x ## debugg
while read inline; do
[ "$inline" = "" ] && continue # blank line
[ ${inline:0:1} = "#" ] && continue # comment
fld2=$(echo $inline |awk '{print $2'})
[ "$fld2" = "localhost" ] && continue # 127.0.0.1 has no longname
checkfq=$(echo $fld2 |awk 'END{print index($0,".")}')
[ $checkfq -gt 0 ] && continue # dot found, valid line
outline=$(echo "$inline" |awk '{print $1" "$3" "$2" "$4" "$5" "$6" "$7" "$8}')
echo "$outline" |tee -a $outfile
done <$infile
echo ''
echo '====='
ls -alh $outfile
exit;
# REF: https://unix.stackexchange.com/questions/153339/how-to-find-a-position-of-a-character
$ echo $tmp1;echo $tmp2
nodot
hasdot.fq.dn
$ echo $tmp1 |awk 'END{print index($0,".")}'
0
$ echo $tmp2 |awk 'END{print index($0,".")}'
7
Sample output:
$ time fixetchosts.sh
255.255.255.255 broadcasthost
10.9.28.24 davesimac-2.series.org imacdual imacold oldimacdualcore
192.168.1.4 cubie.series.org cubietruck-wifi pihole
10.9.0.4 cubietruck-devuan.series.org cubietruck-devuan
=====
-rw-r--r-- 1 dave wheel 241B Mar 2 15:42 /tmp/hosts.fixed
real 0m0.139s