Click to See Complete Forum and Search --> : Obtaining a dynamic Ip and feeding it into a alias


Init-0
06-08-2003, 01:23 PM
Ok im not sure if its alias but ill show you what im trying to do.

Ive managed to obtain the bash script i need and ill write out the relevant part



#obtain the ip address

/sbin/ifconfig | fgrep P-t-P | cut -c21-c35 > IP_Addr



would this work if i was talking about my ip addr in a script and called it eg



iptables -A INPUT -s $IP_Addr -j DROP



cheers for any feedback (i would try it but its not for this comp and the other comp down for now)

ta

jscott
06-08-2003, 01:37 PM
In bash you can use something like:

EXTERNAL_IP=`/sbin/ifconfig | fgrep P-t-P | cut -c21-c35`
echo "\$EXTERNAL_IP: $EXTERNAL_IP"
/sbin/iptables -A INPUT -s $IP_Addr -j DROP

Init-0
06-08-2003, 01:42 PM
cheers mate, thats been buggin me for a while

if im on dhcp ill need to rerun it every time i reconnect wont i?

sharth
06-08-2003, 01:43 PM
debian:/home/sharth# /sbin/ifconfig | grep "inet addr" | cut -c21-35
192.168.1.102
192.168.1.101
127.0.0.1 Mask
debian:/home/sharth#

your code gives no response for me.

Init-0
06-08-2003, 01:47 PM
yeah my code doesnt work :P

im really new to bash scripting

sharth
06-08-2003, 01:55 PM
I really don't know any bash scripting. so heres the idea that im using...

echo `/sbin/ifconfig | grep "inet addr" | cut -c16- ` | cut -c6-

then take from that output untill you get to a " ". there is ip number 1. keep going until you find a "addr:" after that is ip number 2. and keep going. :)

that's the best thing i cna think of, since the length of an ip can change.

Init-0
06-08-2003, 02:03 PM
Ok none of those work

I can feed it into a file if i use



echo 'ip address'

/sbin/ifconfig | fgrep P-t-P | cut -c21-35 > /tmp/session_ip



but i cant seem to feed it into a variable or something

Init-0
06-08-2003, 07:45 PM
I searched high and low on google and found this little code snippit on linux gazzette.

Its not pure bash (some awk and sed) but it does the trick.

Just incase anyone needs to know here it is.

[code]

PPP_DEV="ippp0"

GETMYIP=$(/sbin/ifconfig | grep -A 4 $PPP_DEV \
| awk '/inet/ { print $2 } ' | sed -e s/addr://)

Just incase anyone else needs it

cheers for the help anyway :)

jscott
06-09-2003, 12:59 PM
Not *pure* bash ;) but this works for me

EXTERNAL_IP=`ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d ' ' -f 1`

if you replace eth0 with ppp0 that might work for you.

Are you still having trouble assigning the ip to a variable? Or are you just not seeing the variable outside of the scope of the script? You might want use export to make it externally visable.

export EXTERNAL_IP=`ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d ' ' -f 1`