Other Help Topics :: Set network settings on boot



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.

This is a good idea.

I assume that it works as designed.

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.

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.

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.
Next Page...
original here.