#!/bin/bash HostIP="" Netmask="" Gateway="" doconfig=n get_interface(){ if [ -z $netdevice ];then ifnames=`hwinfo --netcard | grep 'Device File:' | cut -f2 -d: | sed 's/[ \t][ \t]*//g;s/\n/ /'` if [ -z $ifnames ];then echo Sorry no network interfaces found, perhaps you don\'t have an appropriate echo kernel module loaded exit fi for interface in $ifnames do if [ `ethtool $interface | grep 'Link detected' | cut -f2 -d: | sed 's/[ \t][ \t]*//g'` == yes ];then echo Found link on $interface netdevice=$interface return fi done echo Couldn\'t find a link on any interface echo check cabling and run again exit else echo netdevice specified as $netdevice fi } check_ip() { # IP address should only contain [0-9.] if [ :`echo $1| sed 's/[0-9.]//g'`: != :: ];then return 1 fi address=`echo $1 | sed 's/\./ /g'` if [ `echo $address | wc -w` -ne 4 ];then return 1 fi for octet in $address do if [ $octet -ge 0 -a $octet -le 255 ];then /bin/true else return 1 fi done } get_hostip(){ while [ -z $HostIP ] do echo -n Please type in your IP address and press enter: read TempIP check_ip $TempIP; if [ $? -ne 0 ];then echo That didn\'t look valid else HostIP=$TempIP fi done } get_netmask(){ while [ -z $Netmask ] do echo -n Please type in your netmask and press enter: read TempIP check_ip $TempIP; if [ $? -ne 0 ];then echo That didn\'t look valid else Netmask=$TempIP fi done } get_gateway(){ while [ -z $Gateway ] do echo -n Please type in your gateway address and press enter: read TempIP check_ip $TempIP; if [ $? -ne 0 ];then echo That didn\'t look valid else Gateway=$TempIP fi done } report_config(){ echo You have requested interface $netdevice to be configured as follows echo "IP Address :" $HostIP echo "Netmask :" $Netmask echo "Gateway :" $Gateway response=-1 while [ $response -lt 0 ] do echo -n Do you wish to continue [Y/N]: read response if [ -z $response ];then response=z fi response=`echo $response | cut -c1 | tr '[A-Z]' '[a-z]'` if [ $response == y ];then response=0 doconfig=y elif [ $response == n ];then response=1 doconfig=n else response=-1 fi done if [ $doconfig == n ];then HostIP="" Netmask="" Gateway="" fi } do_config(){ ifconfig $netdevice inet $HostIP netmask $Netmask route add -net default gw $Gateway $netdevice } get_interface while [ $doconfig == n ] do get_hostip get_netmask get_gateway report_config done do_config