File: drbl-etc-hosts

package info (click to toggle)
drbl 5.7.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,984 kB
  • sloc: sh: 43,522; perl: 8,820; xml: 867; makefile: 131
file content (87 lines) | stat: -rwxr-xr-x 2,523 bytes parent folder | download | duplicates (5)
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
#!/bin/bash
# Author: Steven Shiau <steven _at_ clonezilla org>
# License: GPL
#

# Load DRBL setting and functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions

# Basic settings
drblpush_conf="/etc/drbl/drblpush.conf"
HOSTS_OUT="/etc/hosts"

# Functions
get_general_param_in_drblpush_conf() {
  local index="$1"
  param="$(grep -E "^$index=" $drblpush_conf | sed -e "s/$index=//g")"
  echo $param
}

DEV_INT="$(grep "^interface=" $drblpush_conf | sed -e "s/interface=//g")"
# use echo to convert them into one line instead of many lines.
DEV_INT="$(echo $DEV_INT)"

# get_general_param_in_drblpush_conf domain
hostname_prefix="$(get_general_param_in_drblpush_conf hostname)"

if [ -f /etc/hosts ]; then
  echo -n "Backup the original /etc/hosts as /etc/hosts.drblsave... "
  cp -f /etc/hosts /etc/hosts.drblsave
  echo "done!"
fi

#
if ! grep -q -E "^127.0.0.1[[:space:]]+" $HOSTS_OUT; then
  cat <<-EOF >> $HOSTS_OUT
127.0.0.1 localhost localhost.localdomain
EOF
fi
#
# Part I: for server
echo "Generate the $HOSTS_OUT... "
for interface in $DEV_INT; do
  srv_ip="$(drbl-get-ipadd $interface)"
  netmask_sys="$(drbl-get-netmask $interface)"
  if grep -q -E "^$srv_ip[[:space:]]+" $HOSTS_OUT; then
    # found the old one, replace that	  
    perl -pi -e "s|^$srv_ip\s.*|$srv_ip ${hostname_prefix}${interface}|g" $HOSTS_OUT
  else
    # old one does not exist, create one
    cat <<-EOF >> $HOSTS_OUT
$srv_ip ${hostname_prefix}${interface}
EOF
  fi
done

# II: for clients
ALL_IP="$(get-client-ip-list)"
for ip in $ALL_IP; do
  # get the hostname from $IP_HOST_TABLE
  label="$(LC_ALL=C grep -iEw "^[[:space:]]*${ip}" $IP_HOST_TABLE | awk -F" " '{print $2}')"
  if [ -z "$label" ]; then
    # Same rules as that in drblpush. i.e. if label is empty, we will use the calculated name based on hostname prefix and IP address.
    label_default="${hostname_prefix}""${ip//./-}"
    label_assigned="$(get-assigned-hn-by-ip $ip)"
    if [ -n "$label_assigned" ]; then
      label=$label_assigned;
    else
      label=$label_default;
    fi
  fi
  
  if grep -q -E "^$ip$" $HOSTS_OUT; then
    # found the old one (only IP with new line ending), replace that
    perl -pi -e "s|^$ip$|$ip $label|g" $HOSTS_OUT
  elif grep -q -E "^$ip[[:space:]]+" $HOSTS_OUT; then
    # found the old one, replace that	  
    perl -pi -e "s|^$ip\s.*|$ip $label|g" $HOSTS_OUT
  else
    # old one does not exist, create one
    cat <<-EOF >> $HOSTS_OUT
$ip $label
EOF
  fi
done
echo "done!"