loop-nb ~ # cat net-loop
#!/bin/bash
# By loopx (loop_x@hotmail.com)...
# If you have bug report or any suggestions, send me a mail :)
# Last modification: 01/15/2006
#####################################
###################################
################################
# Introduction:
# I have made this little script to save ALL network configuration
# of my notebook. Yes, it will not be usefull for desktop computer,
# because they have one configuration, because a tower can't move like
# a notebook.
# So, you can easily use this script to store your different configurations
# in ONE and unique file, which is a script.
#
# A notebook has generally one wired network interface, and one wireless network interface.
# So I use 2 variable: WLAN and LAN, to store the name of the interface (you can found it by this 'ifconfig -a', or 'iwconfig').
# It's very important to set WLAN and LAN. If not, the script will do nothing good :(.
# The 3rd variable are CURRENT. You MUST set it AT THE FIRST LINE OF EACH PROFILE. It would associate a type of connection (LAN, WLAN)
# to the profile. Like this, this script is able to configure your NIC correctly.
# After setting the CURRENT variable, you MUST USE the function 'start' to start initialisation and fixing old route problem if needed.
# Note, you should use (in your profile) $WLAN or $LAN
# instead of eth1 or eth0. It's a modularity question... And if you buy a new wireless card, in addition of the old, and you want to
# use it for ALL your profile, you just need to change the value of the variable WLAN (WLAN="eth3"). So, enjoy ;)
# NOTE: do not forgot to set the DEFAULT value, with the profile name of your profile than you want to set as default. For use the default
# profile, you just need to type 'net-loop' with no argument. By default, DEFAULT will show you the help...
#########################################################################################################################################
########################################################################################################################################
# Example of a very simple profile, to connect to the network 10.59.13.75/24 with 10.59.13.254 as default route (no wireless, no dhcp):
#-------
# my_profile_name() {
# CURRENT="LAN"
# start
# echo "I start my profile here"
# ifconfig $LAN 10.59.13.75 netmask 255.255.255.0 broadcast 10.59.13.255 #no need to add the 'up' argument, 'start' function will do it before
# route add default gw 10.59.13.254
# echo "#my_profile_name" > $DNS #Add a trace to the DNS configuration file, AND replace the current content of this file ('>')
# echo "domain my_domain.org" >> $DNS #Add the domain name AT THE END of the file ('>>') (if not needed, you can remove this line)
# echo "nameserver 10.59.13.75" >> $DNS #Add primary domain name, you can add more by copy/past this line just after...this line
# }
#
# Second example: here is an other profile, but using wireless AND dhcp
#---------------
# my_profile_name() {
# CURRENT="WLAN"
# start
# echo "Here is my second profile"
# iwconfig $WLAN essid "home" key open KEY rate auto AP mac_AP #YOU NEED THIS, before using DHCP with wireless !!! If not, DHCP will fail.
# use_dhcp
# }
##################################################################################################################################################
#################################################################################################################################################

############
# Configuration
#--------------
# 0. Set up the 4 variables: LAN, WLAN, DEFAULT, DNS with correct value ...
#
# 1. You MUST ADD these two command AT THE BEGINING OF EACH PROFILE!!!:
# CURRENT="X" (X= LAN or WLAN)
# start to start the initialisation of your NIC
# 2. You can add this line to use the dpcpcd command for use the DHCP:
# use_dhcp
# 3. If you won't use DHCP, you can now add your command line and argument to set up
# your interface. You can use what you want:
# iwconfig $WLAN ....
# ifconfig $LAN ....
# 4. Now it's stuff time. You can add what you need in addition of your configuration of interface.
# mount serveur:/mnt/data /mnt/server
# xmms ?
# /usr/games/bin/q3demo ...
#
#----------------------------------
# LAN = interface for wired network
# WLAN = interface for wireless network
# DEFAULT = the default profile to use when no parameter or when fixing bug
# DNS = the path to the resolv.conf configuration file (DNS=/etc/resolv.conf by default)
# CURRENT = LAN or WLAN Specifie what interface to use (for DHCP use). This variable MUST BE SET IN THE BEGINING OF YOUR PROFILE
LAN="eth0"
WLAN="eth1"
DEFAULT="home"
DNS=/etc/resolv.conf
############

############
# ALL YOUR PROFILES,
# ARE JUST A LIST OF COMMAND TO EXECUTE IN A SHELL.
# YOU CAN ADD WHAT YOU WANT HERE, LIKE NFS MOUNTING, ETC...
############

# to show the current configuration of your interface
# Now, you can use 'net-loop status vpn' for the status
# of your vpn connection in example
status() {
if [ -z $PARAM2 ]; then
echo ">>> Status of your network interfaces :"

echo " >>> ===LAN interface==="
ifconfig $LAN

echo " >>> ===WLAN interface==="
ifconfig $WLAN
iwconfig $WLAN

#Here is my add of vpn status check. You can remove it if
#not necessary. If you want to add another, just add the same first line (elif [ ...)
#at the end of the first added status (vpn), and BEFORE the 'else'.
elif [ $PARAM2 = "vpn" ]; then
echo ">>> Status of you VPN network :"
echo
echo " Entrez le nom ou l'ip de votre serveur: "
read SERVEUR
#the command run with ssh, show the status of my server OpenVPN
echo
echo " >>> OpenVPN status for" $SERVEUR
ssh root@$SERVEUR cat /etc/openvpn/serveur/openvpn-status.log
else
echo "!!! Error after 'status'. Nothing to do..."
fi
}

#Check the interface, and down it for remove old route. Used when changing profile, if you have manipulated the routing table before switch to another one.
fix_route() {
#check for second parameter "fix"
if [ -z $PARAM2 ]; then
echo " >>> First connection. Use 'fix' for remove old route problems."
elif [ $PARAM1 = "fix" ]; then
echo " >>> Fixing old routes"