I am trying to write a set of scripts for my Ubuntu 810 Server to allow a user to change from DHCP to a user-supplied Static IP, Subnet, Gateway and DNS Servers.
So far I have the following, where $USERCHOICE is a user-entered value in each case:
ifconfig eth0 down && ifconfig eth0 $USERCHOICE && ifconfig eth0 up;;
ifconfig eth0 down && ifconfig netmask $USERCHOICE && ifconfig eth0 up;;
ifconfig eth0 down && route add default gw $USERCHOICE && ifconfig eth0 up;;
ifconfig eth0 down && echo "nameserver $USERCHOICE" > /etc/resolv.conf && ifconfig eth0 up;;
Problems:
1.It doesn't work :), even after issuing /etc/init.d/networking restart
2. Is there an easy way to, from a script, take multiple values and run each command using the appropriate value rather than running each component as a different option?
3. How do I specify 2 DNS Servers instead of 1?
I suspect someone out there is already doing this, and in a much more straightforward fashion that I am attempting to
Thanks for any help.
Alex
The following code of mine (quoted in extracts) has been used to allow for updating the network settings of a Debian-based live system at boot time ...:
eth0|eth1)
updateConsole
echo -ne "\e[33m"
echo ""
echo " \|||/ "
echo " (0 0) "
echo "-------ooO-(_)-Ooo-----------------------"
echo ""
echo "Attempting to upset, er, setup <$OPTION> ..."
echo ""
ADDRESS="192.168.110.100"
echo -n "Address [192.168.110.100]: "
read address
if [ "$address" != "" ]
then
ADDRESS="$address"
fi
GATEWAY="192.168.110.101"
echo -n "Gateway [192.168.110.101]: "
read gateway
if [ "$gateway" != "" ]
then
GATEWAY="$gateway"
fi
echo ""
echo -ne "\e[0m"
ETH=/etc/network/interfaces
mv $ETH $ETH.dhcp
ifconfig lo 127.0.0.1
route add 127.0.0.0 lo
ifconfig "$OPTION" "$ADDRESS" netmask 255.255.255.0
route add "$ADDRESS" "$OPTION"
route add default gw "$GATEWAY" "$OPTION" > /dev/null 2>&1
DNS=/etc/resolv.conf
mv $DNS $DNS.dhcp && touch $DNS
echo "nameserver 208.67.220.220" >> $DNS
echo "nameserver 208.67.222.222" >> $DNS
ping -c 1 housisms.net > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo -ne "\e[32m"
echo "You're now part of the 'World Wide Wait'."
SAVE="yes"
else
echo -ne "\e[31m"
echo "Nice try - to no avail, I'm (not) afraid."
SAVE="no"
fi
echo ""
echo -ne "\e[0m"
if [ "$SAVE" = "yes" ]
then
BROADCAST="`echo $ADDRESS | awk -F '.' '{ print $1"."$2"."$3".255"}'`"
touch $ETH
echo "auto lo" >> $ETH
echo "iface lo inet loopback" >> $ETH
echo "auto $OPTION" >> $ETH
echo "iface $OPTION inet static" >> $ETH
echo "address $ADDRESS" >> $ETH
echo "netmask 255.255.255.0" >> $ETH
echo "broadcast $BROADCAST" >> $ETH
echo "gateway $GATEWAY" >> $ETH
fi
;;