if dhcp else static ip?


Forum: Networking
Topic: if dhcp else static ip?
started by: chaostic

Posted by chaostic on Sep. 25 2006,06:32
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?

Posted by dtf on Sep. 25 2006,12: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.

Posted by chaostic on Sep. 25 2006,20:26
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

Posted by ^thehatsrule^ on Sep. 26 2006,03:36
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

Posted by dtf on Sep. 26 2006,19:54
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.

Posted by chaostic on Sep. 27 2006,00:21
Hmm. Well, eth0 is recognized after I use the netcard config script to setup an ip if I boot and no dhcp address is assigned at start. I don't think it is a problem if the card isn't initialized unless it finds an ip.

Maybe its because I'm using a pcmia ethernet card?

Posted by dtf on Sep. 27 2006,00:44
Quote

Maybe its because I'm using a pcmia ethernet card?


Yes. Maybe that is it.  I am not familar with that configuration.  I was testing using an internal ethernet card.

There are many way you could write the script but I think the ifconfig command is what would drive your check.  Like or something close to this

ETH0=

ETH0=`ifconfig | grep eth0`
if [  -z ${ETH0}  ]; then
   setup static ip.

etc.

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