#!/bin/sh # # Copyright (C) 2006-2007 Lars Engels # Use and distribute under BSD license. # usage() { echo -e "Usage: `basename $0` [-I ]\nUse -h or --help for more help.\n" exit 1 } help() { echo "`basename $0` displays the current network configuration, such as IP/MAC address, subnetmask, default route etc." echo "The output can be used in cooperation with other scripts to set or display the network configuration." echo -e "Usage: `basename $0` [-I ]\n" echo "Options can be one of the following:" echo -e "\t-d/--dhcp-running\n\tChecks wether dhclient is running or not. Returns 'true' or 'false'\n" echo -e "\t-g/--default-gateway\n\tReturns the default gateway\n" echo -e "\t-h/--help\n\tDisplays this help\n" echo -e "\t-i/--ip-address\n\tReturns the interface's IP address. Together with -I\n" echo -e "\t-s/--subnetmask\n\tReturns the interface's subnetmask. Together with -I\n" echo -e "\t-m/--mac-address\n\tReturns the interface's MAC address. Together with -I" } if [ -z "$1" ]; then usage fi if [ $# -eq 1 ]; then case $1 in -d|--dhcp-running) if [ -n "`pgrep dhclient 2>&1`" ]; then echo "true" else echo "false" fi ;; -g|--default-gateway) /sbin/route -n get default | awk '$1 ~ /gateway/ {print $2}' ;; -h|--help) help ;; *) usage ;; esac elif [ $# -lt 4 ] && [ $2 = "-I" ]; then if [ ! "`ifconfig $3 2>/dev/null`" ]; then echo "There is no such interface $3" exit 3 fi case $1 in -i|--ip-address) /sbin/ifconfig $3 inet | awk '$1 ~ /inet/ {print $2}' ;; -s|--subnetmask) /sbin/ifconfig $3 inet | awk '$1 ~ /inet/ {print $4}' ;; -m|--mac-address) /sbin/ifconfig $3 ether | awk '$1 ~ /ether/ {print $2}' ;; *) echo "Unrecognized Option: $1" exit 2 ;; esac else usage fi exit 0