#!/bin/sh if [ -z "$1" ]; then echo "Usage: ping_discovery " exit 1 fi INTERFACE="$1" IPMAPFILE="$2" SCHEME_DIR=/var/lib/laptop-net/schemes export PATH=/bin:/usr/bin:/sbin:/usr/sbin #----------------------------------------------------------------------------- # Allow for extremely long negotiation times with stoopid switches sleep 30 #------------------------------------------------------------------------------ # Load LAPTOP-NET definitions and routines # . /usr/share/laptop-net/shared.sh #------------------------------------------------------------------------------ # Load available schemes from /etc/laptop-net/ip-map. SCHEME=`grep -v -E "(^$|#)" ${IPMAPFILE} | awk '{print $1}'` if [ -z "$SCHEME" ]; then echo "@NO-CHOICES@" exit 1 fi #------------------------------------------------------------------------------ # Test interface configuration: if already configured, return scheme; if # unknown configuration, bring the interface down; if not configured, continue IFTEST=`ifconfig | grep -A 1 "${INTERFACE}" | tail -1 | sed -e "s/:/ /" | awk '{print $3}'` if [ -n "${IFTEST}" ]; then for s in ${SCHEME}; do IFACE=${SCHEME_DIR}/$s ADDRESS=`grep "address" ${IFACE} | awk '{print $2}'` if [ "${IFTEST}" = "${ADDRESS}" ]; then echo $s exit 0 fi done ifconfig ${INTERFACE} down fi #------------------------------------------------------------------------------ # Loop over all non-DHCP schemes, try to bring up the interface and ping the # gateway for s in ${SCHEME}; do IFACE=${SCHEME_DIR}/$s DHCP=`grep "dhcp" $IFACE` if [ -z "$DHCP" ]; then ADDRESS=`grep "address" ${IFACE} | awk '{print $2}'` NETMASK=`grep "netmask" ${IFACE} | awk '{print $2}'` BROADCAST=`grep "broadcast" ${IFACE} | awk '{print $2}'` GATEWAY=`grep "gateway" ${IFACE} | awk '{print $2}'` ifconfig ${INTERFACE} ${ADDRESS} netmask ${NETMASK} broadcast ${BROADCAST} up ping -r -c 1 ${GATEWAY} &> /dev/null SUCCESS=$? ifconfig ${INTERFACE} down if [ "${SUCCESS}" = 0 ]; then echo $s exit fi fi done #------------------------------------------------------------------------------ # Try to bring up the interface using DHCP (somewhat ugly since the DHCLIENT # does not exit on failures but brings up the interface anyway) for s in ${SCHEME}; do IFACE=${SCHEME_DIR}/$s DHCP=`grep "dhcp" $IFACE` if [ -n "$DHCP" ]; then /sbin/dhclient-2.2.x -e ${INTERFACE} &> /tmp/DHCLIENT.log SUCCESS=`grep "DHCPACK" /tmp/DHCLIENT.log` kill `pidof dhclient-2.2.x` ifconfig "${INTERFACE}" down rm -f /tmp/DHCLIENT.log if [ -n "${SUCCESS}" ]; then echo $s exit fi fi done #------------------------------------------------------------------------------ # No Network echo "@NO-RESPONSES@" exit 0