forked from oZONo32/EHCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateHostsFile.sh
executable file
·36 lines (27 loc) · 1.09 KB
/
updateHostsFile.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
#!/bin/bash
# Hosts File Manager for EHCP
# Written by Eric Arnol-Martin ([email protected])
if [ -z "$1" ]; then
echo -e "You must send a string containing the host file entries related to EHCP"
else
# Creates a backup or the current host file
if [ ! -e /etc/hosts_backup ]; then
cp /etc/hosts /etc/hosts_backup;
fi
# If the EHCP Comment does not exist, add it:
ehcpComment="# EHCP Generated Entries # ehcp.net # Place Custom Edits Above This Line Or They WILL BE LOST"
ehcpLocate=$(cat /etc/hosts | grep -i "$ehcpComment")
if [ -z "$ehcpLocate" ]; then
echo -e "\n$ehcpComment" >> /etc/hosts
fi
# Looks for the EHCP comment and returns all lines in the hosts file above it and resaves the host file minus ehcp entries
nonEHCPEntries=$( sed -n "1,/$ehcpComment/ p" /etc/hosts | grep -vi "$ehcpComment")
if [ ! -z "$nonEHCPEntries" ]; then
echo -e "$nonEHCPEntries" | tee /etc/hosts > /dev/null
# Add EHCP records to host file
echo -e "\n$ehcpComment" >> /etc/hosts
echo -e "$1" >> /etc/hosts
else
echo -e "Your hosts file is empty and messed up! Please fix it!"
fi
fi