Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (2) </ [1] 2 >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: if dhcp else static ip?, How can I set ip depending on dhcp?< Next Oldest | Next Newest >
chaostic Offline





Group: Members
Posts: 328
Joined: Mar. 2005
Posted: Sep. 25 2006,06:32 QUOTE

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?
Back to top
Profile PM 
dtf Offline





Group: Members
Posts: 196
Joined: Nov. 2005
Posted: Sep. 25 2006,12:30 QUOTE

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.
Back to top
Profile PM 
chaostic Offline





Group: Members
Posts: 328
Joined: Mar. 2005
Posted: Sep. 25 2006,20:26 QUOTE

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
Back to top
Profile PM 
^thehatsrule^ Offline





Group: Members
Posts: 3275
Joined: July 2006
Posted: Sep. 26 2006,03:36 QUOTE

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
Back to top
Profile PM 
dtf Offline





Group: Members
Posts: 196
Joined: Nov. 2005
Posted: Sep. 26 2006,19:54 QUOTE

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.
Back to top
Profile PM 
6 replies since Sep. 25 2006,06:32 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (2) </ [1] 2 >/
reply to topic new topic new poll
Quick Reply: if dhcp else static ip?

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code