Network Switching Script
I know that there are things like quckswitch and ifplugd out there to help with changing networks, but I never quite got them to work the way I wanted. So here is what I came up with. Right now, it uses ifstate from the standard (K)Ubuntu install. Any suggestions would be great!
#!/bin/bash
############################################
# Network Configuration Switching Program
#
# Description: This program is used to switch network profiles.
# Profiles are given user-defnied names and settings. The
# program will stop all running interfaces and delete the default
# route. It will then go to the desired profile and bring up the
# interface with its settings. It must be ran as root; I use "sudo"
#
# Usage: "sudo switch <profile name>"
#
# Interfaces: eth0 = wired; eth1 = wireless
#
# Copyright (C) 2005 James C. Russell Jr.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
############################################
# Check user input to see if no profile was given
# If no profile is given, inform the user and exit
if [ -z $1 ]
then
echo "Please enter a profile to switch to"
echo "home_wireless"
echo "home_wired"
echo "osu_wireless"
echo "roam_wireless"
echo "roam_wired"
echo "brian_wireless"
exit 0
fi
# Copy the user input to a variable
# I hate using $1, $2, etc in programs
profile=$1
# Define wired and wireless interfaces
# These will probably be different for every system
wired="eth0"
wireless="eth1"
# Check each interface to see if it is running
wired_status=`cat /etc/network/ifstate | grep $wired`
wireless_status=`cat /etc/network/ifstate | grep $wireless`
# If so, then bring it down
if [[ $wired_status == "$wired=$wired" ]]
then
ifconfig $wired down
fi
if [[ $wireless_status == "$wireless=$wireless" ]]
then
ifconfig $wireless down
fi
# Delete the default route
route del default
# This profile is for our house wireless
# This uses static IP and 64-bit WEP
if [ $profile == "home_wireless" ]
then
ifconfig $wireless up
ifconfig $wireless netmask 255.255.255.0 192.168.1.3 up
route add default gw 192.168.1.1 $wireless
iwconfig $wireless essid YmBg
iwconfig $wireless key 6E036DA2D7
# This profile is for our house wired
# This uses static IP
elif [ $profile == "home_wired" ]
then
ifconfig $wired netmask 255.255.255.0 192.168.1.3 up
route add default gw 192.168.1.1 $wired
# This profile is for OSU wireless
# This uses DHCP and no WEP
elif [ $profile == "osu_wireless" ]
then
ifconfig $wireless up
iwconfig $wireless essid OSUWIRELESS
dhclient $wireless
# This profile is for a generic wireless connection
# This uses DHCP, grabs any ESSID, and no WEP
elif [ $profile == "roam_wireless" ]
then
ifconfig $wireless up
iwconfig $wireless essid any
dhclient $wireless
# This profile is for a generic wired connection
# This uses DHCP
elif [ $profile == "roam_wired" ]
then
ifconfig $wired up
dhclient $wired
# This profile is for Brian's wireless
# This uses DHCP and 128-bit WEP
elif [ $profile == "brian_wireless" ]
then
ifconfig $wireless up
iwconfig $wireless essid geekman79
iwconfig $wireless key D3E4FE6E5F9368AC64BD30E177
dhclient $wireless
# If the profile is none of the above, inform the user and exit
else
echo "Invalid profile entered"
exit 0
fi
exit 0
Last edited by 65_289; 07-06-2005 at 01:11 PM.
|