-
Notifications
You must be signed in to change notification settings - Fork 1
/
hostsctl
201 lines (173 loc) · 4.07 KB
/
hostsctl
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
#!/bin/bash
end='\033[0m'
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
white='\033[0;37m'
export hostf="/etc/hosts"
pre=$1
if [[ $UID -ne 0 ]] && ! [[ $pre =~ (-h|--help) ]]; then
echo -e "${red}Cannot run without super user privileges!${end}" && exit
fi
if [[ ! -f $hostf ]] && [[ "$pre" != "--restore" ]]; then
echo -e "${red}hosts file not found!${end}" && exit
fi
bak_file=$HOME/.hostsctlbak
if [[ "$pre" != "--restore" ]]; then
cp -f $hostf $bak_file
fi
if [[ $# -eq 0 ]];then
echo "no valid inputs"
exit
fi
opt=$1
export ip="$2"
export host="$3"
valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
add_hostto_ip() {
local ip=$1
local host=$2
if [[ $(grep -cw "$ip" $hostf) -eq 0 ]]; then
echo "$ip $host" >> $hostf
return 0
else
sed -i "s/$ip\b/$ip $host/" $hostf
return 0
fi
}
check_duplicate() {
local host=$1
dup_count=$(grep -cw "$host" $hostf)
if [[ $dup_count -gt 0 ]]; then
echo -e "${red} This hostname already exists in the hosts file! ${end}\n\n Use -fa option to add a host forcefully!"
exit
fi
}
remove_host() {
local ip=$1
line=$(grep -w "$ip" $hostf)
cip=$(echo $line | awk '{print $1}')
check=$(echo $line | sed -e "s/${ip}\b//" -e "s/$cip\s//g" | sed 's/\s//g')
if [[ "${check:0:1}" == '#' ]] || [[ -z "$check" ]]; then
sed -i "/$ip\b/d" $hostf
return 0
else
sed -i "s/${ip}\b\s*//1" $hostf
return 1
fi
}
remove_ip() {
local ip=$1
sed -i "/$ip\b/d" $hostf
return 0
}
force_addhost() {
local ip=$1
local host=$2
remove_host "$host"
add_hostto_ip "$ip" "$host"
}
wexit() {
sleep 1;exit
}
check_record() {
record=$1
if [[ $(grep -cw "$record" $hostf) -eq 0 ]]; then
echo -e "${yellow}No matching entries for $record!${end}"
wexit
fi
}
case "$opt" in
"-a"|"--add"|"-fa"|"--force-add")
if [[ -n "$ip" ]] && [[ -n "$host" ]]; then
if valid_ip $ip; then
if valid_ip "$host"; then
echo -e "${red}Host cannot be an ip address!${end}"
wexit
fi
if [[ "$opt" == "-fa" ]] || [[ "$opt" == "--force-add" ]]; then
force_addhost "$ip" "$host"
echo -e "${green}Forcefully added host $host${end}"
wexit
fi
check_duplicate "$host"
add_hostto_ip "$ip" "$host"
if [[ $? -eq 0 ]]; then
echo -e "${green}host $host added successfully!${end}";
else
echo -e "${red}Failed to add the host $host!${end}";
fi
else
echo -e "${red}not a valid ip (make sure ip is the first option value)${end}"
fi
else
echo -e "${red}option values are incomplete${end}"
fi
;;
"-rh"|"--remove-host")
if [[ -z $ip ]]; then
echo -e "${red}Host cannot be empty!${end}";
wexit
fi
if valid_ip "$ip"; then
echo -e "${red}Host name cannot be an IP!${end}";
wexit
fi
check_record "$ip"
remove_host "$ip"
echo -e "${green}host $ip was removed!${end}"
wexit
;;
"-ri"|"--remove-ip")
if [[ -z $ip ]]; then
echo -e "${red}IP cannot be empty!${end}"; wexit
fi
check_record "$ip"
remove_ip "$ip"
echo -e "${green}IP $ip was removed!${end}"
wexit
;;
"--restore")
if [[ -f $bak_file ]]; then
cp -f $bak_file $hostf
echo -e "${green}Restored hosts file successfully!${end}"
wexit
fi
echo -e "${red}Backup file not found!${end}"
;;
"-h"|"--help")
echo -e "
${blue}=======================${green}[[ USAGE ]]${blue}=======================${white}
adding a host\t\t\t: hostsctl -a <ip> <hostname>
\t\t\t\t\thostsctl --add <ip> <hostname>
-------\n
adding host (forced)\t\t: hostsctl -fa <ip> <hostname>
\t\t\t\t\thostsctl --force-add <ip> <hostname>
-------\n
removing by host\t\t: hostsctl -rh <hostname>
\t\t\t\t\thostsctl --remove-host <hostname>
-------\n
removing by ip\t\t\t: hostsctl -ri <ip>
\t\t\t\t\thostsctl --remove-ip <ip>$
-------\n
restore last change\t\t: hostsctl --restore${end}
"
;;
*)
echo "no options received / invalid option"
esac