-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-to-etc-hosts.sh
executable file
·55 lines (46 loc) · 1.04 KB
/
add-to-etc-hosts.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
#!/bin/bash
##
## addhost.sh
##
## Made by heow
## Login <[email protected]>
##
## Started on Thu Jun 5 14:09:04 2008 heow
## Last update Thu Jun 5 14:09:04 2008 heow
##
if [ -z "$2" ] ; then
echo "usage: $0 ipaddress hostname"
exit 1
fi
export HOSTFILE="/etc/hosts"
export IPADDR=$1
export HOSTNAME=$2
export TMPFILE=`mktemp -p /tmp addhost.XXXXXXX`
# check each line of output to see if...
export EDITED=0
while read line ; do
# is line commented out?
if [ "#" == "`echo \"\${line%${line#?}}\"`" ] ; then
echo "$line" >> $TMPFILE
continue;
fi
# is the hostname already in there?
if echo $line | grep "\ $HOSTNAME" > /dev/null ; then
# echo $* >> $TMPFILE
# export EDITED=1
echo "#${line}" >> $TMPFILE
continue
fi
# is the ip already in use?
if echo $line | grep "^${IPADDR}\ " > /dev/null ; then
echo "#${line}" >> $TMPFILE
continue
fi
# everything else
echo $line >> $TMPFILE
done < $HOSTFILE
if [ 0 == $EDITED ] ; then
echo $* >> $TMPFILE
fi
sudo cp $TMPFILE $HOSTFILE
rm $TMPFILE