LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxQuestions.org Member Success Stories (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/)
-   -   After some hard time, a script to return your IP (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/after-some-hard-time-a-script-to-return-your-ip-137358/)

frandalla 01-22-2004 12:31 AM

After some hard time, a script to return your IP
 
I don't know if people will find this interesting, but I got so excited I needed to share this with you guys... I needed a script to return me only my ip in a determined interface, but I didn't know anything about awk... so, I got some reading on man awk :study: and after a long and hard time :scratch: I came up with this :

code - Script name: getmyip

#! /bin/sh
DEV=$1
if [ ! $DEV ]; then
DEV=eth1
fi
IP=`ifconfig | grep $DEV -A 1 | grep "inet addr" | awk '/addr/ {FS = ":"; split($2,a,":"); print a[2]}'`
echo "$IP"

end code


Just put that on my /usr/bin and when I need my ip address just type getmyip eth1 for my NAT ip and getmyip ppp0 for my internet IP (PPPoE dsl)

hope this can be helpful... If anyone has a better and easier solution please! tell me :D

bhaskie 01-22-2004 09:28 AM

This will get ur IP without needing to know awk:
# ifconfig | grep eth0 -A 1 | grep addr: | cut -d : -f 2 | cut -d " " -f 1

frandalla 01-22-2004 10:06 AM

Hey, Thanks! Not really used to commands like cut and paste... :)

Crazed123 01-29-2004 01:15 PM

Or you could type 'hostname -i'.


All times are GMT -5. The time now is 02:20 PM.