11

Context: On *nix systems, one may get the IP address of the machine in a shell script this way:

ifconfig | grep 'inet' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'

Or this way too:

ifconfig | grep 'inet' | grep -v '127.0.0.1' | awk '{print $2}' | sed 's/addr://'

Question: Would there be a more straightforward, still portable, way to get the IP address for use in a shell script?

(my apologies to *BSD and Solaris users as the above command may not work; I could not test)

Eric Platon
  • 9,819
  • 6
  • 41
  • 48
  • 2
    What if the machine has more than one NIC? – John Saunders Mar 02 '10 at 08:28
  • 1
    What do you mean by "the IP address" ? It's very common these days to have two or more IP addresses, even on a workstation... – Paul R Mar 02 '10 at 08:29
  • Yes, I thought about this issue. But it depends on what the scripts need to do. For my individual needs, I usually pass the interface name to ifconfig (i.e. `ifconfig eth0`, etc.). I thought that would make the question too specific. Any suggestion how to make the question more meaningful then? Thanks! – Eric Platon Mar 02 '10 at 08:46

7 Answers7

12

you can do it with just one awk command. No need to use too many pipes.

$ ifconfig | awk -F':' '/inet addr/&&!/127.0.0.1/{split($2,_," ");print _[1]}'
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • Your proposal is very fine. Simple tests show that it is also faster on average than my basic proposals. I would like to accept yours for now. Portability is not better though, due to `_[1]` and `$2`. For instance, I need to switch the indices under *BSDs (ah, could test it this time). Honestly, I still wonder if there is a tool to get the IP in one-shot, perhaps passing parameters (e.g. interface name). `ifconfig` is indeed to "configure network interface parameters", not get parameters... Perhaps a well-spread version of `showip.c`, as introduced by Tom, would be great. – Eric Platon Mar 03 '10 at 03:06
  • 1
    if you want to consider portability, you might want to try using a socket library from the various programming tools out there, such as Perl, or Python. eg In Python, one can get the IP address using `socket.gethostbyname(socket.gethostname())`. Of course this is just simple example. Many people have come up with platform independent ways of listing interface address using these libraries. so you might want to give it a try. – ghostdog74 Mar 03 '10 at 03:21
  • Ghostdob74's latest comment gave me the following idea: `ifconfig | grep -oP "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"` It is very portable, as it relies only on standard tools and it does not make any assumption on the position of the IP address in the parsed lines. Note: The regex does capture wrong IP addresses, but it does not matter much given that `ifconfig` returns valid addresses. – Eric Platon Mar 05 '10 at 04:07
5

you give direct interface thereby reducing one grep.

ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'
coder
  • 1,069
  • 3
  • 9
  • 18
  • Thanks @coder for the suggestion. I wonder whether we cannot go even further though! After all *nix tools do something simple and do it well. So there might be a standard way to get the IP address of an interface. For example, something like `hostname --ip-address` (this does not work) would be great. – Eric Platon Mar 02 '10 at 08:59
  • `ifconfig eth0 | grep inet | cut -d: -f2 | cut -d' ' -f1` - this way you do away with heavyweight awk. Of course won't work with ppp0 etc, but if you have more than one NIC you would get more than one answer with your solution too. (head -1 to grab first?) – SF. Mar 02 '10 at 09:08
3

Based on this you can use the following command

ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'
Community
  • 1
  • 1
Ahmad Yoosofan
  • 961
  • 12
  • 21
1

Look here at the Beej's guide to networking to obtain the list of sockets using a simple C program to print out the IP addresses using getaddrinfo(...) call. This simple C Program can be used in part of the shell script to just print out the IP addresses available to stdout which would be easier to do then rely on the ifconfig if you want to remain portable as the output of ifconfig can vary.

Hope this helps, Best regards, Tom.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • Thanks Tom for the pointer; it does help. A mine of information. The `showip.c` addresses the problem (removing the output formatting). I wonder though whether there is a way to relying on "standard" *nix tools in shells. For the context, I am writing some scripts that are supposed to work on *nix and *BSD. It works, but my way is not very elegant. Using `showip.c`, I would need to deploy the program together with the script, which I believe may be avoided. – Eric Platon Mar 02 '10 at 09:03
0

ifconfig | grep 'broadcast\|Bcast' | awk -F ' ' {'print $2'} | head -n 1 | sed -e 's/addr://g'

David J Merritt
  • 161
  • 1
  • 2
0

May be this could help.

 more /etc/hosts | grep `hostname` | awk '{print $1}'
0
# for bash/linux
ipaddr(){
if="${1:-eth0}"
result=$(/sbin/ip -o -4 addr show dev "${if}" | sed 's/^.*inet // ; s/\/...*$//')
printf %s "${result}"
tty -s  && printf "\n"
}
figtrap
  • 154
  • 5