Main Menu

Contact Andrew Quinn

jaquinn@ihug.co.nz http://twitter.com/jaquinn
Ubuntu 8.1 - Network Configuration Change Script
Written by Administrator   
Monday, 05 April 2010 07:54

Ubuntu 8.1 works really well as a build environment for OpenWRT and programming environment for the DSL502T. 

I have found that to make OpenWRT a network connection with Internet access is required but to program the DSL502T using adam2flash-502T.pl minimizing network traffic is important to prevent hangs during programming.

Also all the DSL502T units I have seen use 192.168.1.1 as the IP address for programming... but my internal network using 10.x.x.x network addresses.

So I needed a quick/easy way to switch between DHCP assignment of addresses for the internal network and STATIC assignment when programming.

Directly editing /etc/network/interfaces and restarting the network services works but gets old fairly quickly.... instead I wrote the following setnet script which is called passing DHCP or STATIC as the parameter.  It changes the network configuration and restarts the networking services.

#!/bin/sh

# Check if parameters are provided

if [ $# -ne 1 ]
then
    echo "Must specify STATIC or DHCP"
    exit 1
fi

INTERFACES="/etc/network/interfaces"
ETHMODE=`echo $1 | tr [:lower:] [:upper:]`

# Force both interface types to be off

sudo sed -i "s/#iface eth0 inet dhcp/iface eth0 inet dhcp/" $INTERFACES
sudo sed -i "s/#iface eth0 inet static/iface eth0 inet static/" $INTERFACES
sudo sed -i "s/iface eth0 inet dhcp/#iface eth0 inet dhcp/" $INTERFACES
sudo sed -i "s/iface eth0 inet static/#iface eth0 inet static/" $INTERFACES

# Now select the one that is required

if [ $ETHMODE = "STATIC" ]
then
    echo "setting STATIC"
    sudo sed -i "s/#iface eth0 inet static/iface eth0 inet static/" $INTERFACES
    sudo /etc/init.d/networking restart
elif [ $ETHMODE = "DHCP" ]
then
    echo "setting DHCP"
    sudo sed -i "s/#iface eth0 inet dhcp/iface eth0 inet dhcp/" $INTERFACES
    sudo /etc/init.d/networking restart
else
    echo "Unknown network mode"
fi
 

Probably no awards for great linux shell scripting but it works for me.