View previous topic :: View next topic |
Author |
Message |
The Dark Tux's lil' helper


Joined: 01 Feb 2003 Posts: 126 Location: BACK ON PLANET GENTOO
|
Posted: Sat Feb 15, 2003 2:43 pm Post subject: Code to detect DHCP given IP |
|
|
Hello again.
I'm getting my IP from a DHCP server (router) but i wanted a piece of code to get my IP, as it's changes every time i boot up (yeah yeah i know what your thinking).
The piece of code that i have at the moment is like this
Code: |
## Determine external IP
EXTIP="`ifconfig $EXTIF | grep inet | cut -d : -f 2 | cut -d \ -f 1`"
if [ "$EXTIP" = '' ]; then
echo "Aborting: Unable to determine the IP-address of $EXTIF !"
exit 1
fi
echo External IP: $EXTIP
|
The problem with this code is that it gives me 2 ip's.
The one i get from the DHCP server and my lo IP 127.0.0.1
Hmm is there a nother way to do this ?
Oh yeah i'm trying to write a iptables startup script.
And yeah i didn't write the code myself its copied from another script
Anybody _________________ -=The Dark=-
Linux Rules
i686 Pentium III (Coppermine) GenuineIntel
http://www.gentoo.org |
|
Back to top |
|
 |
pilla Bodhisattva


Joined: 07 Aug 2002 Posts: 7731 Location: Underworld
|
Posted: Sat Feb 15, 2003 3:51 pm Post subject: |
|
|
Code: |
## Determine external IP
EXTIP="`ifconfig $EXTIF | grep inet | cut -d : -f 2 | cut -d \ -f 1 | sed /127.0.0.1/D `"
if [ "$EXTIP" = '' ]; then
echo "Aborting: Unable to determine the IP-address of $EXTIF !"
exit 1
fi
echo External IP: $EXTIP
|
_________________ "I'm just very selective about the reality I choose to accept." -- Calvin |
|
Back to top |
|
 |
fifo Guru


Joined: 14 Jan 2003 Posts: 437
|
Posted: Sat Feb 15, 2003 4:06 pm Post subject: |
|
|
You don't need sed, you just need to set EXTIF to the interface that you want the ip of, for example eth0 or ppp0. |
|
Back to top |
|
 |
The Dark Tux's lil' helper


Joined: 01 Feb 2003 Posts: 126 Location: BACK ON PLANET GENTOO
|
Posted: Sat Feb 15, 2003 4:11 pm Post subject: |
|
|
Bloody Bastard wrote: | Code: |
## Determine external IP
EXTIP="`ifconfig $EXTIF | grep inet | cut -d : -f 2 | cut -d \ -f 1 | sed /127.0.0.1/D `"
if [ "$EXTIP" = '' ]; then
echo "Aborting: Unable to determine the IP-address of $EXTIF !"
exit 1
fi
echo External IP: $EXTIP
|
|
Sorry Bloody but i'm getting an syntax error with this piece of code,, can't figure out what's wrong with it.
 _________________ -=The Dark=-
Linux Rules
i686 Pentium III (Coppermine) GenuineIntel
http://www.gentoo.org |
|
Back to top |
|
 |
pilla Bodhisattva


Joined: 07 Aug 2002 Posts: 7731 Location: Underworld
|
Posted: Sat Feb 15, 2003 4:18 pm Post subject: |
|
|
What is the error? It runs OK for me.
Try using copy-and-paste to create the file. Try also specifying /sbin/ifconfig instead of ifconfig. _________________ "I'm just very selective about the reality I choose to accept." -- Calvin |
|
Back to top |
|
 |
The Dark Tux's lil' helper


Joined: 01 Feb 2003 Posts: 126 Location: BACK ON PLANET GENTOO
|
Posted: Sat Feb 15, 2003 4:23 pm Post subject: |
|
|
Ok let me break it down to yah all.
It's like this.
I'm behind one of those hardware router boxes.
I checked it at http://www.grc.com and came up with a lot of open ports.(cheap box)
know i don't have nothing running on this box except for GENTOO so i decided to chek it out at http://scan.sygate.com and it was stealth al the way.
what i want is to make a iptables script to make things tight just in case.
But i reboot a lot (yeah even linux) and evevry time it's a new ip.(there are other pc's hanging on this box)
Had a script working but had to change IP every time.
Want a script code that could do this automaticly...
LET ME KNOW  _________________ -=The Dark=-
Linux Rules
i686 Pentium III (Coppermine) GenuineIntel
http://www.gentoo.org |
|
Back to top |
|
 |
jda n00b

Joined: 11 Feb 2003 Posts: 5
|
Posted: Mon Feb 17, 2003 9:13 am Post subject: |
|
|
Presuming that your only real interface is eth0, add a line just following the comment to define the interface's name. If $EXTIF lacks a value, ifconfig(8) gets called with no options, which is why you are getting all your interfaces (eth0, lo) listed in the output.
Code: |
## Determine external IP
EXTIF="eth0" # ADD THIS LINE
EXTIP="`ifconfig $EXTIF | grep inet | cut -d : -f 2 | cut -d \ -f 1 `"
if [ "$EXTIP" = '' ]; then
echo "Aborting: Unable to determine the IP-address of $EXTIF !"
exit 1
fi
echo External IP: $EXTIP |
If you had multiple interfaces that changed each time, you could define multiple interfaces in the variable, and use a 'for' loop to iterate over each:
Code: |
## Determine external IP
EXTIF="eth0 eth1"
for IF in $EXTIF
do
EXTIP=`ifconfig $IF | grep inet | cut -d : -f 2 | cut -d \ -f 1 `
if [ "$EXTIP" = '' ]; then
echo "Aborting: Unable to determine the IP-address of $EXTIF !"
exit 1
fi
echo "Interface: $IF IP:$EXTIP"
EXTIP='' # clear for next iteration
done |
|
|
Back to top |
|
 |
|