Set network settings on boot


Forum: Other Help Topics
Topic: Set network settings on boot
started by: friedgold

Posted by friedgold on May 05 2005,13:14
I thought it would be handy to set your network settings at boot via a cheatcode. This is mainly for people with no dhcp server. To do this I edited /etc/init.d/knoppix-autoconifg as follows

find the section starting

Code Sample
if checkbootparam "nodhcp"; then
...


and change it to

Code Sample
if checkbootparam "nodhcp"; then
echo " ${BLUE}Skipping DHCP broadcast/network detection as requested on boot commandline.${NORMAL}"
else
NETDEVICES="$(awk -F: '/eth.:|tr.:/{print $1}' /proc/net/dev 2>/dev/null)"
for DEVICE in $NETDEVICES
do
echo " ${GREEN}Network device ${MAGENTA}$DEVICE${GREEN} detected${NORMAL}"
NETSETTINGS="$(getbootparam $DEVICE 2>/dev/null)"
if [ -n "$NETSETTINGS" ]; then
IP="$(echo "$NETSETTINGS" | cut -d , -f 1)"
NM="$(echo "$NETSETTINGS" | cut -d , -f 2)"
DG="$(echo "$NETSETTINGS" | cut -d , -f 3)"
NS="$(echo "$NETSETTINGS" | cut -d , -f 4)"
NETSETTINGS_ERR=""
for i in $IP $NM $DG $NS; do
[ -n "$i" ] && [ -z "$(echo "$i" | awk '/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/')" ] && NETSETTINGS_ERR="yes"
done
if [ -n "$NETSETTINGS_ERR" ]; then
echo " ${BLUE}Invalid network settings, skipping network configuration.${NORMAL}"
else
ifconfig $DEVICE $IP netmask ${NM:-255.255.255.0}  >/dev/null 2>&1
sleep 1
[ -n "$DG" ] && route add default gw $DG >/dev/null 2>&1
[ -n "$NS" ] && echo "nameserver $NS">/etc/resolv.conf
echo " ${GREEN}Using Static IP:${YELLOW} $IP${NORMAL}"
fi
else
trap 2 3 11
pump -i $DEVICE >/dev/null 2>&1 &
trap "" 2 3 11
sleep 1
echo " ${BLUE}(Backgrounding)${NORMAL}"
fi
done
fi


I then remastered as normal

You can then use the cheatcode eth0=ip,nm,dg,dns where ip is your IP address, nm your netmask, dg your default gateway and dns your dns server. Only the ip address is required, the rest is optional (i.e. you can use eth0=192.168.1.100 to get you network up, although without a gateway/dns server you won't be able to get on the internet). In theory if you have more than one network card you can use eth0=... eth1=... etc to set each one up.

Posted by cbagger01 on May 05 2005,21:19
This is a good idea.

I assume that it works as designed.

Posted by friedgold on May 06 2005,20:12
Quote (cbagger01 @ May 05 2005,17:19)
This is a good idea.

I assume that it works as designed.

Well it works fine for me. Im sure it could be improved/tidied up though.

Does any one know how the best way to check if a string is a valid ip address?

[ -z "$(echo "$i" | awk '/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/')" ]

should check the string is in the form ###.###.###.### where the #'s are nos, but it doesn't rule out stuff like 999.999.999.999.

Posted by cbagger01 on May 07 2005,03:18
In my world (rough, minimally functional script creator), if you don't follow directions and you type in stuff incorrectly, then you deserve to get the unpredictable results that are a consequence of your actions.

I guess this is what separates me from real programmers who take into account the moron factor.

I think that the new feature is good enough as-is, even if you don't do any idiot-proof error checking.

But error checking is cool too, if you know how to do it.

Posted by Fallen Kell on May 27 2005,18:57
hmmm... trying to figure out what I did wrong. It didn't detect my network device and as such didn't do any of the setup by reading the info from the line.
Posted by clacker on May 28 2005,00:28
Quote (friedgold @ May 06 2005,16:12)
Does any one know how the best way to check if a string is a valid ip address?

[ -z "$(echo "$i" | awk '/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/')" ]

should check the string is in the form ###.###.###.### where the #'s are nos, but it doesn't rule out stuff like 999.999.999.999.

Here is a neat way I saw using the cut command to parse out the string:

Code Sample
#!/bin/bash
# pass the ip string as an argument to this script

if echo $1 | grep -q "^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$"
then
   for i in 1 2 3 4
   do
       export dotpart=` echo $1 | cut -f $i -d '.' `
       if [ $dotpart -lt 0 -o $dotpart -gt 255 ]
       then
           echo "ip address was bad"
           exit 1
       fi
   done
   echo "ip address was good"
else
   echo "ip address was bad"
   exit 1
fi
exit 0

Posted by Fallen Kell on May 31 2005,15:02
Hey, I found my problem, well most of it anyway. This is really nice, since I have to find a way to get this to work with a frugal install and need network to come up automatically.

I will probably do some tweaking, but it looks good. Thanks for doing most of the hard work.

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.