#!/bin/sh # Shell script scripts to read ip address # ------------------------------------------------------------------------- # Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- # Get OS name OS=`uname` IO="" # store IP case $OS in Linux) IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;; FreeBSD|OpenBSD) IP=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;; SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;; *) IP="Unknown";; esac echo "$IP"
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- 10 Greatest Open Source Software Of 2009
- My 10 UNIX Command Line Mistakes
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.
- Download Script
- Email this to a friend
- Rss Feed
- Last Updated: 04/10/08
{ 1 trackback }
{ 14 comments… read them below or add one }
Another way, but I think that is better, because in your way, it will give you your local IP, like 192.168.0.101…
you need to have “links2″ already intalled
#!/bin/sh
IP_ACTUAL=`links2 -dump http://www.whatismyip.com | grep “Your IP ” | awk ‘{ print $5 }’`
echo $IP_ACTUAL
And also it doesn’t work in Ubuntu 8.04 in another languages, like spanish
If one must parse ifconfig what about:
ifconfig | egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' | egrep -v '255|(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})'
I think I would like that more then cutting with awk or cut. Also I don’t think it matters what *nix system you are running ifconfig on.
Also in the above example grep -v ‘127.0.0.1′ the periods are not literal since they are not escaped, but rather they match any character.
-Kyle
You should replace “ifconfig” with “/sbin/ifconfig” normaly /sbin isn’t in $PATH.
/sbin/ifconfig eth0 | awk ‘/inet/ {print $2}’ | sed ’s/addr://’
Hai,
I tried to print the HWaddress in interface as like in the above script..i m unable to print..can u plz help me to print the HWaddress as like inet addr..
eth1 Link encap:Ethernet HWaddr 00:0C:FC:00:3A:7E
inet addr:17.1.1.150 Bcast:17.255.255.255 Mask:255.0.0.0
Try
ifconfig eth0 | grep HWaddr | awk -F'HWaddr' '{ print $2}'for ubuntu:
ifconfig | grep 'inet adr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'I wrote a post how to extract IP Address with 4 different tools – sed, awk, perl 5.8 and perl 5.10:
http://www.catonmat.net/blog/golfing-the-extraction-of-ip-addresses-from-ifconfig/
Pete
A very simple way
who | cut -d”(” -f2 | cut -d”)” -f1
Enjoy
Paul
To Kyle Brandt:
This will no work on Solaris as there is no -o option for egrep on said platform. Much as I love the gnu toolset, the unix toolset often has less advanced features so easy commands become harder.
To Paul Avilles:
This does not get you the IP address of the machine. It gets you the IP address of the machine that you are logged in from – not really helpful.
Sorry to be so negative :(
Tom…
The problem is a bit wider because of internationalization issues.
I use
ifconfig $IF | grep -i inet | head -n 1 | sed ’s/[:a-zA-Z]//g;s/\.//’ | awk ‘{ print $1 }’
that will handle ifconfig output for all (or almost) occidental languages under any nix I know of.
Suggestions are welcome. :-)
here’s how I got the IP ADDRESS
ADDRESS=`ifconfig eth0 | grep “intet addr” | tr ‘ ‘ ‘\n’ | grep “addr” | tr -d “addr:”`
echo $ADDRESS
#printf “%s\r\n” “$ADDRESS”
then I had another shell script that called that I could have included it all in one but I did different things at different times when the customer wanted to switch between DHCP and staic
ifconfig eth0 down
ifconfig eth0 up
./filename.sh > ip.txt
chmod 0666 ip.txt
This way I could read ip.txt into my c application and always display the address in the customers software.
1. Can you write a short shell to find any IPV4 IP addresses in any files under /var/lib/*. Perform a dns reverse. Lookup for each IP found, and format the output neatly, like “IP=192.168.0.1, hostname=jo.blogg.com. http://jo.blog.com“?