#!/bin/bash

        #NETDEV=eth0
        #use route command to determine default interface
        NETDEV=$(route | sed -n '3p' |  awk '{print $8}')

	ACTIVE_IP=`ifconfig $NETDEV 2>/dev/null | grep -v 127.0.0.1 |  perl -ni -e 'print "$1\n" if (/.*addr\s*:\s*([\d]+\.[\d]+.[\d]+.[\d]+).*/)'`
	if [ -z "$ACTIVE_IP" ]; then
		#if eth0 doesn't have an ip pick the first interface that does excluding loopback 
		ACTIVE_IP=`ifconfig | grep -v 127.0.0.1 |  perl -ni -e 'print "$1\n" if (/.*addr\s*:\s*([\d]+\.[\d]+.[\d]+.[\d]+).*/)' | head -n 1`
	fi

	if [ -z "$ACTIVE_IP" ]; then
		echo "no active ip found" 1>&2
		exit 1;
	else
		echo $ACTIVE_IP;
		exit 0;
        fi

