#!/bin/sh ## # This script finds the current network configuration (if any) # and fill the networking2.conf file for used by asterisk-gui ## AST_NET_CFG=/etc/asterisk/networking2.conf SYS_NET_CFG=/etc/networking.conf IFCONFIG=/sbin/ifconfig ROUTE=/sbin/route ETC_RESOLV=/etc/resolv.conf LAN_INTERFACE=eth0 RELOAD="" ## # Find out if the network is already configured # ## SSHACCESS="" HOSTNAME=`hostname` IP_LAN=`$IFCONFIG eth0 | sed -n 's/^.*inet addr:\([0-9\.]*\).*$/\1/p'` NETMASK_LAN=`$IFCONFIG eth0 | sed -n 's/^.*inet.* Mask:\([0-9\.]*\).*$/\1/p'` GATEWAY_LAN=`/sbin/route -n | sed -n 's/^0\.0\.0\.0 *\([0-9\.]*\).*$/\1/p'` SSH_INIT=`ls /persistent/autorun/S[0-9]*sshd 2> /dev/null` if [ -x "$SSH_INIT" ]; then SSHACCESS="yes" else SSHACCESS="no" fi DHCP_LAN="no" DOMAIN_LAN=`cat /etc/resolv.conf | sed -n 's/^domain *\(.*\) *$/\1/p'` DNS_LAN=`cat /etc/resolv.conf | sed -n 's/^nameserver *\(.*\) *$/\1/p'` ## # Check if the script was called with the "reload" parameter # if [ X$1 = X"reload" ]; then echo "Reloading networking..." RELOAD=yes fi # Rewrite the asterisk network config echo "Copying file to asterisk/networking2.conf" mkdir -p /etc/asterisk echo "[general]" > $AST_NET_CFG cat $SYS_NET_CFG >> $AST_NET_CFG ## # If an ip address is configured, don't do anything... # ...unless we are being called with the 'reload' parameter # if [[ $IP_LAN ]] && [[ -z $RELOAD ]]; then # We got an address from u-boot... overwrite an existing config # We will remove any DHCP_LAN line from the config and replace # IP_LAN echo "Networking was configured from u-boot..." # if [ -e $SYS_NET_CFG ]; then # sed -i '/DHCP_LAN/d' $SYS_NET_CFG # sed -i '/IP_LAN/d' $SYS_NET_CFG # sed -i '/NETMASK_LAN/d' $SYS_NET_CFG # sed -i '/GATEWAY_LAN/d' $SYS_NET_CFG # fi # echo "IP_LAN=$IP_LAN" >> $SYS_NET_CFG # echo "NETMASK_LAN=$NETMASK_LAN" >> $SYS_NET_CFG # echo "GATEWAY_LAN=$GATEWAY_LAN" >> $SYS_NET_CFG # echo "DHCP_LAN=no" >> $SYS_NET_CFG else # Didn't get an address from u-boot or reloading networking # with new config # Load system config and apply it... if [ -e $SYS_NET_CFG ];then echo "Found $SYS_NET_CFG... configuring networking" . $SYS_NET_CFG ifconfig lo 127.0.0.1 # Now for the real logic in the script. # Note: You CANNOT use functions with msh/busybox. # Set our hostname if [ "${HOSTNAME}" = "" ]; then HOSTNAME=pika fi hostname ${HOSTNAME} # Turn on our ssh server if [[ "${SSHACCESS}" = "yes" ]] && [[ -f "/persistent/autorun/S[0-9]*sshd" ]] && [[ ! -x "/persistent/autorun/S[0-9]*sshd" ]]; then chmod +x /persistent/autorun/S[0-9]*sshd /persistent/autorun/S[0-9]*sshd fi # Turn off our ssh server if [[ "${SSHACCESS}" = "no" ]] && [[ -x "/persistent/autorun/S[0-9]*sshd" ]]; then chmod -x /persistent/autorun/S[0-9]*sshd killall dropbear fi # Setup the LAN interface(s). NETWORK_LAN=`echo "${IP_LAN}" | cut -d '.' -f 1,2,3` echo "LAN network is ${NETWORK_LAN}" if [[ "${DHCP_LAN}" == "yes" ]] || [[ -z $IP_LAN ]]; then echo "Running DHCP client..." killall -9 udhcpc 2> /dev/null udhcpc -b else echo ${IP_LAN} ${HOSTNAME} > /etc/hosts # Delete old default gateways in the routing table OLDGATEWAY=`route -n | grep "^0.0.0.0" | grep UG | cut -c 17-32` if [ "${OLDGATEWAY}" != "" ]; then for i in $OLDGATEWAY; do route delete default gw $i; done fi ifconfig ${LAN_INTERFACE} ${IP_LAN} netmask ${NETMASK_LAN} if [ -n "$DNS_LAN" ]; then rm -f $ETC_RESOLV for n in `echo $DNS_LAN` ; do echo nameserver $n >> $ETC_RESOLV done fi if [ -n "$GATEWAY_LAN" ]; then route add default gw $GATEWAY_LAN fi fi else echo "Warning: could not find $SYS_NET_CFG" fi fi