26
if [ -z "$hostname" -o -z "$firstip" ] ; then
27
echo "Usage: set_hostname.sh <hostname> <first_ip>"
26
31
update_etc_hostname() {
27
echo ${hostname} > "${ROOT}/etc/hostname"
32
echo ${hostname} > "${ROOT}/etc/hostname"
30
35
update_etc_hosts() {
32
# We need to be careful not to scribble over the existing entries
33
# in /etc/hosts, but also be thorough enough to actually make it work
34
backupfile="$(make_backup "${ROOT}/etc/hosts")"
35
if ! [ -f "${backupfile}" ]
37
# We need to be careful not to scribble over the existing entries
38
# in /etc/hosts, but also be thorough enough to actually make it work
39
backupfile="$(make_backup "${ROOT}/etc/hosts")"
40
if ! [ -f "${backupfile}" ]
45
found=`awk -v ip=$firstip '$1 == ip { print "found"; exit 0 }' < ${ROOT}/etc/hosts`
46
if [ "x$found" != "xfound" ] ; then
47
echo "$firstip $hostname" >> ${ROOT}/etc/hosts
51
cat "${backupfile}" | while read ip tail
53
if [ "x$ip" = "x${firstip}" ]
39
cat "${backupfile}" | while read ip tail
41
if [ "x$ip" = "x$firstip" ]
43
# If there's just a single hostname, assume it's the one we
45
if echo "$tail" | grep -iq '^[a-z][a-z0-9]*$'
47
echo "#$ip $tail # Removed by Agent Smith"
49
elif echo "$tail" | grep -q '[ ]'
51
# Ok, more than one word in the line..
52
# Without the while loop, a, b, and c are not available
54
echo "$tail" | while read a b c
56
if [ -n "$c" ] && ! echo "$c" | grep -q "^#"
58
# Three words.. Don't know how to handle that.
60
elif echo "$a" | grep -q '\.' && echo echo "$b" | grep -q '\.'
62
# Two words, both with a dot in them.. Don't know how to handle that.
64
elif ! echo "$a" | grep -q '\.' && ! echo echo "$b" | grep -q '\.'
66
# Two words, neither with a dot in them.. Don't know how to handle that.
69
# If there's not a dot in both, nor are both missing a dot,
70
# exactly one of them must have a dot. Yay.
71
withdot="$(echo $a | grep '\.' ; echo $b | grep '\.')"
72
withoutdot="$(echo $a | grep -v '\.' ; echo $b | grep -v '\.')"
73
if [ "${withdot%%.*}" != "${withoutdot}" ]
75
# If the host part doesn't match, I don't know what to do.
78
domain="${withdot#*.}"
79
echo "#$ip $tail # Removed by Agent Smith"
80
echo "$ip $hostname.$domain $hostname"
55
# If there's just a single hostname, assume it's the one we
57
if echo "$tail" | egrep -iq '^[a-z](\-|[a-z0-9])*$'
59
echo "#$ip $tail # Removed by Agent Smith"
61
elif echo "$tail" | grep -q '[ ]'
63
# Ok, more than one word in the line..
64
# Without the while loop, a, b, and c are not available
66
echo "$tail" | while read a b c
68
if [ -n "$c" ] && ! echo "$c" | grep -q "^#"
70
# Three words.. Don't know how to handle that.
72
elif echo "$a" | grep -q '\.' && echo echo "$b" | grep -q '\.'
74
# Two words, both with a dot in them.. Don't know how to handle that.
76
elif ! echo "$a" | grep -q '\.' && ! echo echo "$b" | grep -q '\.'
78
# Two words, neither with a dot in them.. Don't know how to handle that.
81
# If there's not a dot in both, nor are both missing a dot,
82
# exactly one of them must have a dot. Yay.
83
withdot="$(echo $a | grep '\.' ; echo $b | grep '\.')"
84
withoutdot="$(echo $a | grep -v '\.' ; echo $b | grep -v '\.')"
85
if [ "${withdot%%.*}" != "${withoutdot}" ]
87
# If the host part doesn't match, I don't know what to do.
90
domain="${withdot#*.}"
91
echo "#$ip $tail # Removed by Agent Smith"
92
echo "$ip $hostname.$domain $hostname"
90
done > "${ROOT}/etc/hosts"
102
done > "${ROOT}/etc/hosts"
93
105
update_etc_hostname
108
if [ "$ROOT" = "/" -o "$ROOT" = "" ] ; then
109
# only do this if we're not running tests