| 1 | #!/bin/bashscip-shell filesystem . . `/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.confscip-shell filesystem . . `/etc/drbl/drblpush.conf`." |
| 13 | HOSTS_OUT="/etc/hostsscip-shell filesystem . . `/etc/hosts`." |
| 14 | |
| 15 | # Functions |
| 16 | get_general_param_in_drblpush_confscip-shell shell shell-project 0.0.0 get_general_param_in_drblpush_conf().() { |
| 17 | local index="$1" |
| 18 | param="$(grepscip-shell system . . grep. -E "^$index=" $drblpush_conf | sedscip-shell system . . sed. -e "s/$index=//g")" |
| 19 | echoscip-shell system . . echo. $param |
| 20 | } |
| 21 | |
| 22 | DEV_INT="$(grepscip-shell system . . grep. "^interface=" $drblpush_conf | sedscip-shell system . . sed. -e "s/interface=//g")" |
| 23 | # use echo to convert them into one line instead of many lines. |
| 24 | DEV_INT="$(echoscip-shell system . . echo. $DEV_INT)" |
| 25 | |
| 26 | # get_general_param_in_drblpush_conf domain |
| 27 | hostname_prefix="$(get_general_param_in_drblpush_confscip-shell shell shell-project 0.0.0 get_general_param_in_drblpush_conf(). hostname)" |
| 28 | |
| 29 | if [scip-shell system . . `[`. -f /etc/hostsscip-shell filesystem . . `/etc/hosts`. ]; then |
| 30 | echoscip-shell system . . echo. -n "Backup the original /etc/hosts as /etc/hosts.drblsave... " |
| 31 | cpscip-shell system . . cp. -f /etc/hostsscip-shell filesystem . . `/etc/hosts`. /etc/hosts.drblsavescip-shell filesystem . . `/etc/hosts.drblsave`. |
| 32 | echoscip-shell system . . echo. "done!" |
| 33 | fi |
| 34 | |
| 35 | # |
| 36 | if ! grepscip-shell system . . grep. -q -E "^127.0.0.1[[:space:]]+" $HOSTS_OUT; then |
| 37 | catscip-shell system . . cat. <<-EOF >> $HOSTS_OUT |
| 38 | 127.0.0.1 localhost localhost.localdomain |
| 39 | EOF |
| 40 | fi |
| 41 | # |
| 42 | # Part I: for server |
| 43 | echoscip-shell system . . 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 grepscip-shell system . . grep. -q -E "^$srv_ip[[:space:]]+" $HOSTS_OUT; then |
| 48 | # found the old one, replace that |
| 49 | perlscip-shell system . . 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 | catscip-shell system . . 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 grepscip-shell system . . grep. -iEw "^[[:space:]]*${ip}" $IP_HOST_TABLE | awkscip-shell system . . awk. -F" " '{print $2}')" |
| 63 | if [scip-shell system . . `[`. -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 [scip-shell system . . `[`. -n "$label_assigned" ]; then |
| 68 | label=$label_assigned; |
| 69 | else |
| 70 | label=$label_default; |
| 71 | fi |
| 72 | fi |
| 73 | |
| 74 | if grepscip-shell system . . grep. -q -E "^$ip$" $HOSTS_OUT; then |
| 75 | # found the old one (only IP with new line ending), replace that |
| 76 | perlscip-shell system . . perl. -pi -e "s|^$ip$|$ip $label|g" $HOSTS_OUT |
| 77 | elif grepscip-shell system . . grep. -q -E "^$ip[[:space:]]+" $HOSTS_OUT; then |
| 78 | # found the old one, replace that |
| 79 | perlscip-shell system . . perl. -pi -e "s|^$ip\s.*|$ip $label|g" $HOSTS_OUT |
| 80 | else |
| 81 | # old one does not exist, create one |
| 82 | catscip-shell system . . cat. <<-EOF >> $HOSTS_OUT |
| 83 | $ip $label |
| 84 | EOF |
| 85 | fi |
| 86 | done |
| 87 | echoscip-shell system . . echo. "done!" |