Welcome to the CSIR Meraka Institute's "COIN" Blog

Thursday, September 08, 2005

Setting up different networking scenarios on a laptop

I have always wanted to write some good scripts that configure my laptop for home and work wireless/ethernet automatically in ubuntu and so I set about building up a set of scripts that I can call.

I have four scenarios:
1. use laptop at home with wireless access point
2. use laptop at home with ethernet
3. use laptop at work with wireless access point
4. use laptop at work with ethernet

I created two files in /etc/network: interfaces.work and interfaces.home with all the ethernet and wireless settings for home and work in this file

/etc/network/interfaces.home
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
iface eth0 inet static
address 10.3.13.102
netmask 255.255.255.0
gateway 10.3.13.1

#The wireless network interface
iface eth1 inet dhcp
wireless-essid pta-mesh
wireless-mode Ad-Hoc
wireless-channel 1
wireless-key off

/etc/network/interfaces.work
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
iface eth0 inet dhcp

# The wireless network interface
iface eth1 inet dhcp
wireless-essid icomtek
wireless_mode Managed
wireless-key off

I also created two files with my dns and domain settings for home and work in /etc/ called resolv.home and resolv.work

resolv.home
search icomtek.csir.co.za elarduspark.org.za cids.org.za
nameserver 146.64.28.1 10.3.13.1

resolv.work
search icomtek.csir.co.za cids.org.za
nameserver 146.64.28.1


Here are my scripts that configure my interfaces based on the above files

1. Setup for wireless networking at home
/usr/local/bin/homenet-wireless

#!/bin/bash
echo Setting up network for home wireless network
sudo cp /etc/network/interfaces.home /etc/network/interfaces

eth0_status=`ifconfig | grep eth0`
eth1_status=`ifconfig | grep eth1`

if [ -n "$eth0_status" ]; then
sudo ifdown eth0
fi

if [ -n "$eth1_status" ]; then
sudo ifdown eth1
fi

sudo ifup eth1

sudo cp /etc/resolv.home /etc/resolv.conf

2. Setup for ethernet networking at home
/usr/local/bin/homenet-fixed
#!/bin/bash
echo Setting up network for home ethernet
sudo cp /etc/network/interfaces.home /etc/network/interfaces
sudo cp /etc/resolv.home /etc/resolv.conf

eth0_status=`ifconfig | grep eth0`
eth1_status=`ifconfig | grep eth1`

if [ -n "$eth0_status" ]; then
sudo ifdown eth0
fi

if [ -n "$eth1_status" ]; then
sudo ifdown eth1
fi

sudo ifup eth0

3. Setup for wireless at work
/usr/local/bin/worknet-wireless
#!/bin/bash
echo Setting up network for work wireless network
sudo cp /etc/network/interfaces.work /etc/network/interfaces
sudo cp /etc/resolv.work /etc/resolv.conf

eth0_status=`ifconfig | grep eth0`
eth1_status=`ifconfig | grep eth1`

if [ -n "$eth0_status" ]; then
sudo ifdown eth0
fi

if [ -n "$eth1_status" ]; then
sudo ifdown eth1
fi

sudo ifup eth1

4. Setup for ethernet at work
/usr/local/bin/worknet-fixed
#!/bin/bash
echo Setting up network for work ethernet
sudo cp /etc/network/interfaces.work /etc/network/interfaces
sudo cp /etc/resolv.work /etc/resolv.conf

eth0_status=`ifconfig | grep eth0`
eth1_status=`ifconfig | grep eth1`

if [ -n "$eth0_status" ]; then
sudo ifdown eth0
fi

if [ -n "$eth1_status" ]; then
sudo ifdown eth1
fi

sudo ifup eth0

No comments: