| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # |
| 5 | |
| 6 | # Load DRBL setting and functions |
| 7 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 8 | |
| 9 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 10 | |
| 11 | # Basic settings |
| 12 | drblpush_conf="/etc/drbl/drblpush.conf" |
| 13 | HOSTS_OUT="/etc/hosts" |
| 14 | |
| 15 | # Functions |
| 16 | get_general_param_in_drblpush_conf() { |
| 17 | local index="$1" |
| 18 | param="$(grep -E "^$index=" $drblpush_conf | sed -e "s/$index=//g")" |
| 19 | echo $param |
| 20 | } |
| 21 | |
| 22 | DEV_INT="$(grep "^interface=" $drblpush_conf | sed -e "s/interface=//g")" |
| 23 | # use echo to convert them into one line instead of many lines. |
| 24 | DEV_INT="$(echo $DEV_INT)" |
| 25 | |
| 26 | # get_general_param_in_drblpush_conf domain |
| 27 | hostname_prefix="$(get_general_param_in_drblpush_conf hostname)" |
| 28 | |
| 29 | if [ -f /etc/hosts ]; then |
| 30 | echo -n "Backup the original /etc/hosts as /etc/hosts.drblsave... " |
| 31 | cp -f /etc/hosts /etc/hosts.drblsave |
| 32 | echo "done!" |
| 33 | fi |
| 34 | |
| 35 | # |
| 36 | if ! grep -q -E "^127.0.0.1[[:space:]]+" $HOSTS_OUT; then |
| 37 | cat <<-EOF >> $HOSTS_OUT |
| 38 | 127.0.0.1 localhost localhost.localdomain |
| 39 | EOF |
| 40 | fi |
| 41 | # |
| 42 | # Part I: for server |
| 43 | echo "Generate the $HOSTS_OUT... " |
| 44 | for interface in $DEV_INT; do |
| 45 | srv_ip="$(drbl-get-ipadd $interface)" |
| 46 | netmask_sys="$(drbl-get-netmask $interface)" |
| 47 | if grep -q -E "^$srv_ip[[:space:]]+" $HOSTS_OUT; then |
| 48 | # found the old one, replace that |
| 49 | perl -pi -e "s|^$srv_ip\s.*|$srv_ip ${hostname_prefix}${interface}|g" $HOSTS_OUT |
| 50 | else |
| 51 | # old one does not exist, create one |
| 52 | cat <<-EOF >> $HOSTS_OUT |
| 53 | $srv_ip ${hostname_prefix}${interface} |
| 54 | EOF |
| 55 | fi |
| 56 | done |
| 57 | |
| 58 | # II: for clients |
| 59 | ALL_IP="$(get-client-ip-list)" |
| 60 | for ip in $ALL_IP; do |
| 61 | # get the hostname from $IP_HOST_TABLE |
| 62 | label="$(LC_ALL=C grep -iEw "^[[:space:]]*${ip}" $IP_HOST_TABLE | awk -F" " '{print $2}')" |
| 63 | if [ -z "$label" ]; then |
| 64 | # 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. |
| 65 | label_default="${hostname_prefix}""${ip//./-}" |
| 66 | label_assigned="$(get-assigned-hn-by-ip $ip)" |
| 67 | if [ -n "$label_assigned" ]; then |
| 68 | label=$label_assigned; |
| 69 | else |
| 70 | label=$label_default; |
| 71 | fi |
| 72 | fi |
| 73 | |
| 74 | if grep -q -E "^$ip$" $HOSTS_OUT; then |
| 75 | # found the old one (only IP with new line ending), replace that |
| 76 | perl -pi -e "s|^$ip$|$ip $label|g" $HOSTS_OUT |
| 77 | elif grep -q -E "^$ip[[:space:]]+" $HOSTS_OUT; then |
| 78 | # found the old one, replace that |
| 79 | perl -pi -e "s|^$ip\s.*|$ip $label|g" $HOSTS_OUT |
| 80 | else |
| 81 | # old one does not exist, create one |
| 82 | cat <<-EOF >> $HOSTS_OUT |
| 83 | $ip $label |
| 84 | EOF |
| 85 | fi |
| 86 | done |
| 87 | echo "done!" |