LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2005, 12:24 PM   #1
65_289
Member
 
Registered: May 2002
Distribution: Ubuntu 5.04, Kanotix
Posts: 163

Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
switching between/activate network devices snow_bound Mandriva 3 06-04-2005 12:48 AM
KDE 3.3 Bad Monitor Switching on Desktop Switching Inchoate Linux - Software 1 05-25-2005 02:00 PM
General Strategy for switching Windows -> Linux network? mcle Linux - General 0 12-29-2004 11:55 AM
shell script switching user question compupurdy Linux - Newbie 1 11-22-2003 09:03 AM
switching users in a script NGraphiX Linux - General 7 09-25-2002 04:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 11:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration