-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostfile
executable file
·143 lines (126 loc) · 2.9 KB
/
hostfile
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
#!/bin/bash
#####################################################################
## Simple script for listing, adding and removing hostfile entries ##
## ##
## Kevin Bentlage <[email protected]> ##
#####################################################################
# Host File
hostFile="/etc/hosts"
# Usage
# Printing usage information
function usage {
echo -e "\nUsage"
echo -e "Add entry \t hostfile -a <hostname> <ip>";
echo -e "Remove entry \t hostfile -r <hostname>";
echo -e "List \t hostfile -l";
echo -e "Edit hostfile \t hostfile -e";
}
# Show Help
# Show help and usage information
function showHelp {
usage
exit
}
# List Entries
# List all entries in hostfile
function listEntries {
cat "$hostFile"
}
# Add Entry
# Add new entry to hostfile when it not present yet
function addEntry {
# Prepare line
hostLine="$ip\t$hostname\t# Added on $(date +'%m-%d-%Y')"
if [ -n "$(grep $hostname $hostFile)" ]
then
echo -e "$hostname already exists:\n\n$(grep $hostname $hostFile)"
else
echo "Adding $hostname to $hostFile";
sudo sh -c -e "echo '$hostLine' >> $hostFile";
if [ -n "$(grep $hostname $hostFile)" ]
then
echo -e "$hostname was succesfully added\n\n$(grep $hostname $hostFile)";
else
echo "Failed to add $hostname, Try again!";
fi
fi
}
# Remove Entry
# Removes entry from hostfile if exists
function removeEntry {
echo "Removing hostfile entry";
if [ -n "$(grep $hostname $hostFile)" ]
then
echo "$hostname found in $hostFile, Removing now...";
sudo sed -i ".bak" "/$hostname/d" $hostFile
else
echo "$hostname was not found in $hostFile";
fi
}
# Edit Entries
# Edit entries in hostfile
function editEntries {
sudo vi "$hostFile"
}
# Catch attributes
while getopts "a:r:le" opt;
do
case $opt in
l)
action="L"
;;
a)
action="A"
hostname=$OPTARG
eval "ip=\${$OPTIND}"
shift 2
;;
r)
action="R"
hostname=$OPTARG
;;
e)
action="E"
;;
\?)
echo -e "Invalid option: -$OPTARG"
showHelp
;;
esac
done
# When no action is given, show help
if [ -z $action ]; then
showHelp
fi
# List action
if [ $action == "L" ]; then
listEntries
exit
fi
# Add action
if [ $action == "A" ]; then
if [ -z $hostname ]; then
echo -e "Missing argument: <hostname>"
showHelp
fi
if [ -z $ip ]; then
echo -e "Missing argument: <ip>"
showHelp
fi
addEntry
exit
fi
# Remove action
if [ $action == "R" ]; then
if [ -z $hostname ]; then
echo -e "Missing argument: <hostname>"
showHelp
fi
removeEntry
exit
fi
# Edit action
if [ $action == "E" ]; then
editEntries
exit
fi