IP Address command

I am looking for a command that will give me the IP address of the particular machie that I am on.  (Something like the hostname command)
hawhiteAsked:
Who is Participating?
 
chris_calabreseConnect With a Mentor Commented:
The first problem here is that not all machines have an IP address.  The second is that not all machines have only one IP address.

Meanwhile, the following ought to work on most Unix systems...

nslookup `hostname` | awk '
  /^Name:/ { flag=1 }
  flag && /^Address/ {
    gsub( /,/, "")
    for(i=2; i <= NF; i++)
      print $i
  }'
0
 
bkesarlaCommented:
ping -s <hostname> shud fetch you the ip address of the machine..
0
 
chris_calabreseCommented:
This will fetch an IP address of the machine, but will not fetch all IP addresses if there are more than one.  Of course, this may be good enough in some circumstances.  Meanwhile, you'll also want to make sure you have the right ping options for your particular platform.  For example, on HP-UX, you'd use

/usr/sbin/ping -s 1 -c 1 `hostname` | head -1 | cut -d '(' -f 2 | cut -d ')' -f 1
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
filiepCommented:
Try ifconfig -a
This should give the IP addresses for each NIC thats in the server
0
 
chris_calabreseCommented:
Again, this depends on the Unix flavor.  On HP-UX, which does not have ifconfig -a, you'd need to do

lanscan | grep '^[0-9]' | awk '{print $5}' | while read dev
do      ifconfig "$dev"
done | grep inet | awk '{print $2}'
0
 
tspfwjCommented:
ifconfig -a is good to show the addresses, you can also try netstat -in to show the active interfaces and addresses assigned to them
0
 
chris_calabreseCommented:
Ah, hadn't tought of the netstat -in one.  Yes, that would work too.
  netstat -in | awk '$4 ~ /^[0-9]/ && $4 !~ /^127/ { print $4 }'

I don't know of any OS specific tweaking you'd need, but netstat, and it gives you all the IP's, so that makes it one of the better methods.
0
All Courses

From novice to tech pro — start learning today.