How do I get the ip address via a bash script?

7,951

Solution 1

Note that this can output multiple addresses, because "the address" hasn't necessarily been true for many years now.

Linux

Using iproute2 and awk:

ip addr show scope global | awk '$1 ~ /^inet/ {print $2}'
ip -4 addr show scope global | awk '$1 == "inet" {print $2}'
ip -6 addr show scope global | awk '$1 == "inet6" {print $2}'

Using iproute2's recent JSON support:

ip -json addr show scope global | jq -r '.[] | .addr_info | .[] | .local'
ip -json -4 addr show scope global | jq -r '.[] | .addr_info | .[] | .local'
ip -json -6 addr show scope global | jq -r '.[] | .addr_info | .[] | .local'

ip -json addr | jq -r '.[] | .addr_info | .[] | select(.scope == "global") | .local'
ip -json addr | jq -r '.[] | .addr_info | .[] | select(.family == "inet" and .scope == "global") | .local'
ip -json addr | jq -r '.[] | .addr_info | .[] | select(.family == "inet6" and .scope == "global") | .local'

FreeBSD

Using FreeBSD ifconfig and awk (filtering by scope is a bit more difficult here):

ifconfig -a | awk '$1 ~ /^inet/ {print $2}'
ifconfig -a | awk '$1 == "inet" {print $2}'

Also note that ifconfig has many different output styles between different OSes – even Linux has at least three versions.

Solution 2

If you can install moreutils then use ifdata:

ifdata - get network interface info without parsing ifconfig output

[…]

-pa
Prints the IPv4 address of the interface.

Example:

ifdata -pa wlan0            # print the address
ipaddr=$(ifdata -pa wlan0)  # assign to variable

Solution 3

Or just:

ipaddr=$(ifconfig wlan0 | awk '/inet addr:/ {gsub("addr:","",$2); print $2}')
Share:
7,951

Related videos on Youtube

user3346931
Author by

user3346931

Updated on September 18, 2022

Comments

  • user3346931
    user3346931 over 1 year

    I have an application that requires you to input your ip address as a parameter, how to do I use a bash script to automatically get the ip address and have it put it in as a parameter for the program?

    • Admin
      Admin over 8 years
    • Admin
      Admin over 8 years
      Have you thought of using localhost as the ip? Or do you have to give it a specific ip address? Because if it's running on my computer, localhost will work for just my computer, or whichever computer it's running on.
    • Admin
      Admin over 8 years
      hmm, I just want it to get the ip address of wlan0, not the loopback ip address. dakre18, the progarm is a simple network sniffer, and I want the ip address it uses to be my own ip address. Having it sniff the loopback is pointless
    • Admin
      Admin over 8 years
      you could always create a host file entry for any arbitrary name, to map it to your wlan0 IP, but if you really want to programatically define it, please check the SO threads DavidPostill linked.
    • Admin
      Admin over 8 years
      The following works on Ubuntu: ifconfig wlan0|grep inet\ addr|sed -e 's/^.*inet addr://' -e 's/ Bcast:.*$//'. If it doesn't work for you, examine the output of ifconfig wlan0 and change the search fields accordingly. If you need it in a variable, use ip=$(AboveExpression).
    • Admin
      Admin over 8 years
      If instead you want your public ip Command for determining my public IP?... If it works e.g hostname -I you can use ./myApp --IP $(hostname -I) if --IP is the option to give to your program before the IP. Choose a GNU independent solution and use it...
  • Burgi
    Burgi about 7 years
    Can you provide a little more context? Your answer is at risk of being removed.
  • Vomit IT - Chunky Mess Style
    Vomit IT - Chunky Mess Style about 6 years
    Works fine on CentOS 7—seems like best answer to me.