Networking :: if dhcp else static ip?



Basicly, I want to create a script that runs on either boot (Opt/bootlocal.sh) or when starting X (.xintrc). THe script would check to see if a dhcp ip has been assigned, and if so, exit without chaning anything. Else, it assigns a static ip address using the commands listed in the wiki:faq. This is for a hd install, so it should be simpler.

Is there a way to check for a dhcp address specificly?

And also, as a side question, does linux have a feature similar to Mac Os X's "DHCP with Manual address"? Basiclly, manually choose an address within the dhcp pool, but pull the defualt gateway, subnetmask and dns server info from dhcp?

One way might be to allow for dchp (the default boot option) and then use ifconfig to determine if an ip address has been assigned and base your actions on the results.  An example

Quote

IPADDR=

IPADDR=`ifconfig eth0 | grep "inet addr"|cut -d':' -f2|cut -d' ' -f1`

if [ ${IPADDR} = "00.00.00.00" ]; then
   assign your ip
else
   do something else
fi


I am not sure what you get when there is no IP address assigned so you should double check.  Another way you can check is to check for received packets.  

There might be better ways to accomplish this task but this is one idea.

Quote (dtf @ Sep. 25 2006,08:30)
One way might be to allow for dchp (the default boot option) and then use ifconfig to determine if an ip address has been assigned and base your actions on the results.  An example

Quote

IPADDR=

IPADDR=`ifconfig eth0 | grep "inet addr"|cut -d':' -f2|cut -d' ' -f1`

if [ ${IPADDR} = "00.00.00.00" ]; then
   assign your ip
else
   do something else
fi


I am not sure what you get when there is no IP address assigned so you should double check.  Another way you can check is to check for received packets.  

There might be better ways to accomplish this task but this is one idea.

Well, if there's no dhcp assigned address at boot, I think eth0 doesn't get added to ifconfig. It would only show lo (loobback 127.0.0.1). Looking up a nonexistent device in ifconfig results in:

eth1: error fetching interface information: Device not found.

So how would I grep (or awk) it so that if ifconfig eth0 results in device not found, do x, else y?

if [ ${result} = "Device not found" ]; then
    ifconfig eth0 192.168.x.x;
    route add default gw 192.168.x.x;
    echo nameserver 0.0.0.0 > /etc/resolv.conf;
    ifup eth0;
    echo "cde.local Hardware Setup IP defined";
else
   echo "DHCP Address Aquired"
fi

I just need the awk/grep command.

Sidenote, is the subnetmask not needed? It's the plain /24 255.255.255.0

chaostic: dtf gave an example in
Code Sample
IPADDR=`ifconfig eth0 | grep "inet addr"|cut -d':' -f2|cut -d' ' -f1`

I guess you just missed it :P

Quote

Well, if there's no dhcp assigned address at boot, I think eth0 doesn't get added to ifconfig. It would only show lo
(loobback 127.0.0.1). Looking up a nonexistent device in
ifconfig results in:

I tried this last night by unpluging my ethernet cable and booting
into DSL.  What I found was that eth0 was found but there was
no "inet addr" line.  So you need to modify your test to be
Quote

if [ -z $IPADDR -o $IPADDR = "00.00.00.00" ]; then


This test is checking for a null IPADDR or a IPADDR which is
assigned all zeroes.  In either case you would set up a static
IP.  Since I am not at my DSL computer with a bash shell I am not sure of the syntax so it might not be exact.

If you do not see eth0 at all then you may have other issues
because I believe the interface should be recognized as part
of the hardware detection even if you are not able to assign
an IP address dynamicly.  

Quote

Sidenote, is the subnetmask not needed? It's the plain /24 255.255.255.0


I am guessing here.  The first octec 192 is for a class "c" IP address,
so I am guessing that its mask will default to 255.255.255.0 the
default class "c" mask.

Next Page...
original here.