Close
#! /bin/bash

# >>> -------
# Autor: Julien BUTY
# Date: 11/11/2006
# ------- <<<

#-------------------------------------------------------------------------------------------------------#
# >>> CONFIGURATION PART                                                                                #
#-------------------------------------------------------------------------------------------------------#

# >>> LEVEL
#Available values for LEVEL are 'debug', 'info' or 'quiet'
#'debug' display each details on the current running process, such as 'PID', 'cpu time'
#LEVEL="debug"
#'info' display the network configuration - only when 'dhcp' or 'services' are selected
LEVEL="info"
#'quiet' display nothing but the status of the configuration: 'successfull' or 'failed'
#LEVEL="quiet"

# >>> DEVICE
# This is the default device
#Available value for DEVICE are '1' or '2' - '1' stand for eth0, '2' stand for 'eth1'
DEVICE="1"

# >>> WAY
# This is the default way
#Available values for WAY are '1','2' or '3' - '1' stands for dhcp, '2' stands for 'manual', '3' stands for service
WAY="3"

# >>> AUTOCOMP
#Available value for AUTOCOMP are '0' or '1'
#With '0' none IP address suggestion will be displayed
#With '1' a begining of suggestion will be displayed for the gateway's and dns's ip address
#- Note: The suggestions are NOT editable with '1' !
AUTOCOMP="0"

# >>> MSG
#This var contents the message displayed when the net was successfully configured
MSG="Life is good, the net is up :)"

#########################################################################################################
#-------------------------------------------------------------------------------------------------------#
# >>> SCRIPT START HERE - DO NOT MODIFY BELOW UNLESS YOU KNOW WHAT YOU ARE DOING                        #
#-------------------------------------------------------------------------------------------------------#
#########################################################################################################

#For the case where somebody lanch it from a terminal - we check if the user is root
if [ $EUID != 0 ]; then echo "Sorry, you must be the \"root\" user to be able to run this script.";
exit;fi;

echo -n " * Device #1: "
echo `lspci | grep -i eth |cut -d " " -f2-10`
echo -n " * Device #2: "
echo `lspci | grep -i wire |cut -d " " -f2-9`
#echo "  (Device #1 will be selected in 3 sec.)"
device=0
while [ $device -eq 0 ]
#while test $device -ne 2
  do printf "Please select the device you are willing to use [$DEVICE]: "
# for i in 1 2 3; do sleep 1s; device=$DEVICE;done;
# if [ $device=$DEVICE ]; then break;fi;
  read device
  if test $device -z;then device=$DEVICE;break;fi;
done


printf " * Way #1: dhcp"
printf "\n * Way #2: manual"
printf "\n * Way #3: services\n"
#echo "  (Way #3 will be selected in 3 sec.)"
way=0
while [ $way -eq 0 ]
  do printf "Please select the way you are willing your network configuration [$WAY]: " 
# for i in 1 2 3; do sleep 1s;way=$WAY;done;
# if [ $way=$WAY ]; then break;fi;
  read way
  if [ -z $way ]; then way=$WAY;break;fi;
done
echo "device: $device"
echo "way: $way"

#----------------------------------------------------------------------------#
# >>> ETHERNET SECTION                                                       #
#----------------------------------------------------------------------------#
#setting up eth0 with DHCP
if [ $device -eq 1 ] & [ $way -eq 1 ]
then 
  `dhcpcd eth0`
  echo "---"
  echo $MSG
  case "$LEVEL" in
  "debug")
    echo "debug is on!"
  ;;
  "info")
    echo -n "IP address: "
    echo `ifconfig eth0|grep "addr:" | tr -d " "|cut -d ":" -f3|cut -d B -f1`
    echo -n "Netmask: "
    echo ` ifconfig eth0|grep Mask|tr -d " "|cut -d ":" -f5`
    echo -n "Gateway: "
    echo `route |grep default|tr -d "[:alpha:]"|tr " " k |cut -d "k" -f10`
    echo -n "DNS: "
    echo `tail -n 1 /etc/resolv.conf`
  ;;
  "quiet")
    `grep /tmp/*failed`
    if test $? -ne 0
    then echo "Failed to set the network's conf."
    else echo "Network successfuly set up."
    fi
  ;;
  *) echo "LEVEL is *NOT* set!"
  esac
  `rm -fr /tmp/*failed 2>&1 /dev/null`
  exit
fi
#setting up eth0 with 'la mano'
if [ $device -eq 1 ] & [ $way -eq 2 ]
then 
  #initializing vars
  ipaddress=""
  networkmask=""
  gateway=""
  dns=""
  #ip address configuration,here the address...
  while test -z $ipaddress 
  do   printf " Enter your IP address: " 
    read ipaddress
  done
  #...and here the mask
  while test -z $networkmask
  do   printf " Enter your network mask: "
    read networkmask
  done
  #gateway's ip
  while test -z $gateway
  do   echo -n " Enter the gateway's address: "
    if [ $AUTOCOMP -eq 1 ]
    then
      echo -n `echo $ipaddress | cut -d "." -f1-3`
      gateway=`echo $ipaddress | cut -d "." -f1-3`
      echo -n "."
      gateway+="."
      read gatewayend
      gateway+=$gatewayend
    else
      read gateway
    fi
  done
  #dns's ip
  while test -z $dns
  do   echo -n " Enter the name server's address: "
    if [ $AUTOCOMP -eq 1 ]
    then
      echo -n `echo $ipaddress | cut -d "." -f1-3`
      dns=`echo $ipaddress | cut -d "." -f1-3`
      echo -n "."
      dns+="."
      read dsnend
      dns+=$dnsend
    else
      read dns
    fi
  done
  #perform actions
  echo -n "Performing network's configuration..."
  echo -n "0%..."
  `ifconfig eth0 down > /dev/null 2> /dev/null`
  `ifconfig eth0 up > /dev/null 2> /tmp/upfailed`
    if test $? -ne 0;then printf "\n/!\\ Error: Impossible to bring eth0 up\n"; echo " >> Details:";
    echo `cat /tmp/upfailed`; exit;fi;
  echo -n "25%...";
  `ifconfig eth0 $ipaddress netmask $networkmask 2> /tmp/ipfailed`
    if test $? -ne 0;then printf "\n/!\\ Error: Impossible to configure eth0 with the given ip and netmask\n";
    echo " >> Details:"; echo `cat /tmp/ipfailed`; exit;fi;
  echo -n "50%..."
  `route add default gw $gateway 2> /tmp/gatewayfailed`
    if test $? -ne 0;then printf "\n/!\\ Error: Impossible to configure the gateway with the given ip\n";
    echo " >> Details:"; echo `cat /tmp/gatewayfailed`; exit;fi;
  echo -n "75%..."
    DNSPRESENT=`grep "nameserver $dns" /etc/resolv.conf`
    if [ -z $DNSPRESENT ];then `echo "nameserver $dns" >> /etc/resolv.conf 2> /tmp/dnsfailed`;fi;
    if test $? -ne 0;then printf "\n/!\\ Error: Impossible to configure the name server with the given ip\n";
    echo " >> Details:"; echo `cat /tmp/dnsfailed`; exit;fi;
  echo -n "100%..."
  echo "done!"
  #display a nice message
  echo "----"
  echo $MSG
  `rm -fr /tmp/*failed 2> /dev/null`
  exit
fi
#setting up eth0 with the pre-configuration form services
if [ $device -eq 1 ] & [ $way -eq 3 ]
then  echo ""
  echo -n "Performing network's configuration..."
  echo -n "0%..."
  `/etc/init.d/net.eth0 stop 2> /dev/null > /dev/null`
  echo -n "50%..."
  `/etc/init.d/net.eth0 start 2> /tmp/servicefailed > /dev/null` 
  echo -n "100%..."
  #if test $? -ne 0;then printf "\n/!\\ Error: Impossible to lauch this service\n";
  # echo " >> Details:"; echo `cat /tmp/servicefailed`; exit;fi;
  echo " done!"
  echo "----"
  echo $MSG
  case "$LEVEL" in
  "debug")
    echo "debug is on!"
  ;;
  "info")
    echo " >>> INFO"
    echo -n " * IP address: "
    echo `ifconfig eth0|grep "addr:" | tr -d " "|cut -d ":" -f3|cut -d B -f1`
    echo -n " * Netmask: "
    echo ` ifconfig eth0|grep Mask|tr -d " "|cut -d ":" -f5`
    echo -n " * Gateway: "
    echo `route |grep default|tr -d "[:alpha:]"|tr " " k |cut -d "k" -f10`
    echo -n " * DNS: "
    echo `tail -n 1 /etc/resolv.conf | cut -d " " -f2`
  ;;
  "quiet")
    finalstatus=`grep /tmp/*failed`
    if [ $finalstatus -z ] 
    then echo "Failed to set the network up."
    else echo "Network successfuly set up."
    fi
  ;;
  *) echo "LEVEL is *NOT* set!"
  esac
  `rm -fr /tmp/*failed 2> /dev/null`
  exit
fi

#----------------------------------------------------------------------------#
# >>> WIRELESS SECTION                                                       #
#----------------------------------------------------------------------------#
#setting up eth1 with DHCP
if [ $device -eq 2 ] & [ $way -eq 1 ];
then echo "you choosed the wireless and the dhcp but there're not implemented yet";
exit;`dhcpcd eth1 > /dev/null 2> /dev/null`; fi;
#setting up eth1 with 'la mano'
if [ $device -eq 2 ] & [ $way -eq 2 ]
then 
echo "you choosed the wireless and the manual but there're not implemented yet";exit;
  printf "-- wireless module must be already loaded --"
  printf "1) WEP\n2) WPA"; 
  printf "WPA or WEP? [WPA]";  
  read wirelessway
  if [$wirelessway = (1 | WEP)]
  then printf "\nEnter the ESSID:"
    read essid
    printf "\nEnter the mode:"
    read mode
    printf "\nEnter the channel:"
    read channel
    printf "\nEnter the encryption(off for none):"
    read enc
    `iwconfig eth0 essid $essid mode $mode channel $channel enc $enc`
  fi
  if [$wirelessway = (2 | WPA)]
  
  #ip address configuration
  printf "Enter your IP address:" 
  read ipaddress
  printf "\nEnter your network mask:"
  read networkmask
  `ifconfig eth0 $ipaddress netmask $networkmask`
  #gateway configuration
  printf "\nEnter the gateway's address:"
  read gateway
  `route add default gw $gateway`
  #dns configuration
  printf "\nEnter the name server's address:"
  read dns
  `printf nameserver $dns >> /etc/resolv.conf`
  #display a nice message
  printf "Life is nice, your net is up :)"
fi
##############################################################################
#----------------------------------------------------------------------------#
# <<< SCRIPT STOP HERE - hope you enjoyed reading/testing my script :)       #  
#----------------------------------------------------------------------------#
##############################################################################