#!/bin/sh # # Usage: modhosts [load] . /bin/env.mod.rcconf ar7ipaddr () { local SEC=$1 local DEV=$2 echo $(echo 'ar7cfg.'$SEC'['$DEV'].ipaddr' | ar7cfgctl -s) } ar7hostname () { HOSTNAME=$(echo servercfg.hostname | ar7cfgctl -s) HOSTNAME=$(eval echo $HOSTNAME) if [ "$HOSTNAME" != "(none)" ]; then echo $HOSTNAME else echo $CONFIG_HOSTNAME fi } ismac() { echo $1 | egrep -q '^[[:xdigit:]]{2}(:[[:xdigit:]]{2}){5}$' && return 0 } isip4() { echo $1 | egrep -q '^[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}$' && return 0 } isip6() { ## example 111a:222b:333c:444d:555e:666f:77a7:88b8 echo $1 | egrep -q '^(([[:xdigit:]]{1,4}):){7}(([[:xdigit:]]{1,4}))$' && return 0 ## example ::333c:444d:555e:666f:77a7:88b8 | ::88b8 echo $1 | egrep -q '^(:(:([[:xdigit:]]{1,4})){1,6})$' && return 0 ## example 111a:222b:333c:444d:555e:666f:: | 111a:: echo $1 | egrep -q '^((([[:xdigit:]]{1,4}):){1,6}:)$' && return 0 ## example 111a::333c:444d:555e:666f:77a7:88b8 | 111a::88b8 echo $1 | egrep -q '^((([[:xdigit:]]{1,4}):)(:([[:xdigit:]]{1,4})){1,6})$' && return 0 ## example 111a:222b::444d:555e:666f:77a7:88b8 | 111a:222b::88b8 echo $1 | egrep -q '^((([[:xdigit:]]{1,4}):){2}(:([[:xdigit:]]{1,4})){1,5})$' && return 0 ## example 111a:222b:333c::555e:666f:77a7:88b8 echo $1 | egrep -q '^((([[:xdigit:]]{1,4}):){3}(:([[:xdigit:]]{1,4})){1,4})$' && return 0 ## example 111a:222b:333c:444d::666f:77a7:88b8 echo $1 | egrep -q '^((([[:xdigit:]]{1,4}):){4}(:([[:xdigit:]]{1,4})){1,3})$' && return 0 ## example 111a:222b:333c:444d:555e::77a7:88b8 echo $1 | egrep -q '^((([[:xdigit:]]{1,4}):){5}(:([[:xdigit:]]{1,4})){1,2})$' && return 0 } isip() { isip4 $1 && return 0 isip6 $1 && return 0 } create_resolv_conf () { local __hostname=$1 local _nameserver=$(cat /tmp/flash/mod.diff 2>/dev/null| sed -nr "s/.* MOD_RESOLV_DNS='(.*)'/\1/p") [ -z "$_nameserver" ] && local _nameserver="127.0.0.1" echo "nameserver $_nameserver" > /etc/resolv.conf local _domain=$(echo $__hostname |cut -d "." -f2-) [ -n "$_domain" ] && echo "domain $_domain" >> /etc/resolv.conf local _timeout=$(cat /tmp/flash/mod.diff 2>/dev/null| sed -nr "s/.* MOD_RESOLV_TIMEOUT='(.*)'/\1/p") [ -n "$_timeout" ] && echo "options timeout:$_timeout" >> /etc/resolv.conf local _attempts=$(cat /tmp/flash/mod.diff 2>/dev/null| sed -nr "s/.* MOD_RESOLV_ATTEMPTS='(.*)'/\1/p") [ -n "$_attempts" ] && echo "options attempts:$_attempts" >> /etc/resolv.conf } case $1 in "") mvi /tmp/flash/mod/hosts $0 load ;; load) _hostname=$(ar7hostname) hostname $_hostname create_resolv_conf $_hostname rm -f /var/tmp/hosts rm -f /var/tmp/ethers touch /var/tmp/hosts touch /var/tmp/ethers if [ -r /tmp/flash/mod/hosts ]; then last='' egrep -v '^(#|[[:space:]]*$)' /tmp/flash/mod/hosts | sort -k4 | while read -r ip mac interface host desc; do [ -z "$host" -o "$host" = "*" ] && host="pc$(( hnr = hnr + 1))" isip $ip && echo -e "${ip}\t${host} $desc" >> /var/tmp/hosts [ "$last" == "$host" ] && continue || last="$host" ismac $mac && echo -e "${mac}\t${host}" >> /var/tmp/ethers done fi if ! egrep "^127.0.0.1([[:space:]]+[^[:space:]#]+)*[[:space:]]+localhost($|[[:space:]#])" /var/tmp/hosts -q 2>/dev/null; then echo -e "127.0.0.1\tlocalhost" >> /var/tmp/hosts fi ethmode=$(echo 'ar7cfg.ethmode' | ar7cfgctl -s) case $ethmode in ethmode_bridge) devname=lan fallback=eth0 section=brinterfaces break ;; ethmode_router) devname=eth0 fallback=lan section=ethinterfaces break ;; *) devname=eth0 fallback=lan section=ethinterfaces ;; esac ipaddr=$(ar7ipaddr $section $devname) [ -z "$ipaddr" ] && ipaddr=$(ar7ipaddr $section $fallback) [ -z "$ipaddr" ] && ipaddr=169.254.1.1 [ -d /usr/www.nas ] && nas_hostname="fritz.nas\t" if ! egrep "^$ipaddr[[:space:]]" /var/tmp/hosts -q 2>/dev/null; then echo -e "$ipaddr\tfritz.box\t$nas_hostname$_hostname" >> /var/tmp/hosts fi ;; *) echo "Usage: $0 [load]" 1>&2 exit 1 ;; esac exit 0