-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddHosts.sh
executable file
·47 lines (40 loc) · 1.16 KB
/
AddHosts.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
#!/bin/sh
# run:
# ./AddHosts.sh add 127.0.0.1 document_manager_backend
# ./AddHosts.sh remove 10.20.1.2 test.com
# PATH TO YOUR HOSTS FILE
ETC_HOSTS=/etc/hosts
function remove() {
# IP to add/remove.
IP=$1
# Hostname to add/remove.
HOSTNAME=$2
HOSTS_LINE="$IP[[:space:]]$HOSTNAME"
if [ -n "$(grep -P $HOSTS_LINE $ETC_HOSTS)" ]
then
echo "$HOSTS_LINE Found in your $ETC_HOSTS, Removing now...";
sudo sed -i".bak" "/$HOSTS_LINE/d" $ETC_HOSTS
else
echo "$HOSTS_LINE was not found in your $ETC_HOSTS";
fi
}
function add() {
IP=$1
HOSTNAME=$2
HOSTS_LINE="$IP[[:space:]]$HOSTNAME"
line_content=$( printf "%s\t%s\n" "$IP" "$HOSTNAME" )
if [ -n "$(grep $HOSTS_LINE $ETC_HOSTS)" ]
then
echo "$line_content already exists: $(grep $HOSTNAME $ETC_HOSTS)"
else
echo "Adding $line_content to your $ETC_HOSTS";
sudo -- sh -c -e "echo '$line_content' >> /etc/hosts";
if [ -n "$(grep $HOSTNAME $ETC_HOSTS)" ]
then
echo "$line_content was added succesfully";
else
echo "Failed to Add $line_content, Try again!";
fi
fi
}
$@