Reputation:
I'm trying to find a short and robust way to put my IP address into a bash variable and was curious if there was an easier way to do this. This is how I am currently doing it:
ip=`ifconfig|xargs|awk '{print $7}'|sed -e 's/[a-z]*:/''/'`
Upvotes: 46
Views: 96467
Reputation: 2805
assuming you have something like curl
or wget
, then one easy way to get external IP addresses with no downstream parsing necessary would be :
— (wildcard syntax for wget
might differ)
curl -w '\n\n%{url_effective}\n\n' -s 'http://api{,6}.ipify.org'
98.xxx.132.255
http://api.ipify.org/
2603:7000:xxxx:xxxx:xxxx:c80f:1351:390d
http://api6.ipify.org/
Yielding both IPv4
and IPv6
addresses in one shot
Upvotes: 0
Reputation: 221
If by "my ip address" you mean "the IP address my machine will use to get to the public Internet to which I am connected", then this very tidy JSON answer may work for you, depending on what O/S your machine runs:
ip=$( ip -j route get 8.8.8.8 | jq -r '.[].prefsrc' )
It does require an ip
command that produces JSON output (some say the BusyBox ip
command cannot do this), the CLI jq
parser that can extract fields from JSON input, and your machine has to know how to get to the public IP at 8.8.8.8.
If you want the IP address your machine would use to get to some other place, such as a local network, put that other IP in place of the public IP 8.8.8.8, e.g.
ip=$( ip -j route get 192.168.1.1 | jq -r '.[].prefsrc' )
ip=$( ip -j route get 10.1.2.3 | jq -r '.[].prefsrc' )
If you only have one interface, then most any non-localhost IP address should work to get your IP address.
Parsing the JSON output with jq
is so much simpler than all those complex examples with sed
, awk
, and grep
, but the more complex examples do use tools that are present by default on almost all Unix/Linux/BSD systems.
Upvotes: 0
Reputation: 266
On mac osx, you can use ipconfig getifaddr [interface]
to get the local ip:
$ ipconfig getifaddr en0
192.168.1.30
$ man ipconfig
DESCRIPTION
ipconfig is a utility that communicates with the IPConfiguration agent to
retrieve and set IP configuration parameters. It should only be used in
a test and debug context. Using it for any other purpose is strongly
discouraged. Public API's in the SystemConfiguration framework are cur-
rently the only supported way to access and control the state of IPCon-
figuration.
...
getifaddr interface-name
Prints to standard output the IP address for the first net-
work service associated with the given interface. The output
will be empty if no service is currently configured or active
on the interface.
Upvotes: 0
Reputation: 37710
ip
is the right tool to use as ifconfig
has been deprecated for some time now. Here's an awk/sed/grep-free command that's significantly faster than any of the others posted here!:
ip=$(ip -f inet -o addr show eth0|cut -d\ -f 7 | cut -d/ -f 1)
(yes that is an escaped space after the first -d
)
Upvotes: 43
Reputation: 41456
Here is the best way to get IP address of an device into an variable:
ip=$(ip route get 8.8.8.8 | awk 'NR==1 {print $NF}')
NB Update to support new Linux version. (works also with older)
ip=$(ip route get 8.8.8.8 | awk -F"src " 'NR==1{split($2,a," ");print a[1]}')
Why is it the best?
Hostname -I
some times get only the IP or as on my VPS it gets 127.0.0.2 143.127.52.130 2a00:dee0:ed3:83:245:70:fc12:d196
Hostnmae -I
does not work on all system.ifconfig
may not always give the IP you like.Searching of eth0
may fail if interface have other name as in VPS
server or wifi
ip route get 8.8.8.8
Tries to get route and interface to Googles DNS server (does not open any session)
Then its easy to get the ip
or interface name if you like.
This can also be used to get a ip
address of an interface to a host on a multiruted net
Upvotes: 11
Reputation: 176
The "eth3" is optional (useful for multiple NIC's)
ipaddress=`ip addr show eth3 | grep 'inet ' | awk '{ print $2}' | cut -d'/' -f1`
Upvotes: 0
Reputation: 635
ifconfig | grep -oP "(?<=inet addr:).*?(?= Bcast)"
When using grep to extract a portion of a line (as some other answers do), perl look-ahead and look-behind assertions are your friends.
The quick explanation is that the first (?<=inet addr:)
and last (?= Bcast)
parenthesis contain patterns that must be matched, but the characters that match those patters won't be returned by grep, only the characters that are between the two patterns and match the pattern .*?
that is found between the sets of parenthesis, are returned.
Sample ifconfig output:
eth0 Link encap:Ethernet HWaddr d0:67:e5:3f:b7:d3
inet addr:10.0.0.114 Bcast:10.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1392392 errors:0 dropped:0 overruns:0 frame:0
TX packets:1197193 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1294730881 (1.2 GB) TX bytes:167208753 (167.2 MB)
Interrupt:18
This will extract your IP address from the ifconfig output:
ifconfig | grep -oP "(?<=inet addr:).*?(?=Bcast)"
To assign that to a variable, use this:
ip=$(ifconfig | grep -oP "(?<=inet addr:).*?(?=Bcast)")
A slightly more in depth explanation:
From man grep:
-o
Print only the matched (non-empty) parts of matching lines, with each such part on a separate output line.
-P
Interpret the pattern as a Perl regular expression. This is highly experimental and ‘grep -P’ may warn of unimplemented features.
From permonks.org:
(?<=pattern)
is a positive look-behind assertion
(?=pattern)
is a positive look-ahead assertion
-o
Tells grep to only return the portion of the line that matches the pattern. The look-behinds/aheads are not considered by grep to be part of the pattern that is returned. The ?
after .*
is important since we want it to look for the very next look-ahead after the .* pattern is matched, and not look for the last look-ahead match. (This is not needed if we added a regex for the IP address instead of .*, but, readability).
Upvotes: 3
Reputation: 149
The following works on Mac OS (where there is no ip commnand or hostname options):
#!/bin/bash
#get interface used for defalt route (usually en0)
IF=$(route get default |grep 'interface' |awk -F: '{print $2}');
#get the IP address for inteface IF
#does ifconfig, greps for interface plus 5 lines, greps for line with 'inet '
IP=$(ifconfig |grep -A5 $IF | grep 'inet ' | cut -d: -f2 |awk '{print $2}');
#get the gateway for the default route
GW=$(route get default | awk '/gateway:/ {print $2}');
Upvotes: 3
Reputation: 9483
You can take a look at this site for alternatives.
One way would be:
ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
A bit smaller one, although it is not at all robust, and can return the wrong value depending on your system:
$ /sbin/ifconfig | sed -n '2 p' | awk '{print $3}'
(from http://www.htmlstaff.org/ver.php?id=22346)
Upvotes: 24
Reputation: 2449
In my case I had some more interfaces in list before eth0
. By this command you can get ip4 address for any interface. For that you need to change eth0
to interface that you need.
/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'
Upvotes: 0
Reputation: 11
In trying to avoid too many pipes, work on various linuxes, set an exit code, and avoiding ifconfig or other packages, I tried the whole thing in awk:
ip addr show | awk '
BEGIN {FS="/"}
/^[0-9]+: eth[0-9]+.*UP*/ {ss=1}
ss==1 && /^ +inet / {print substr($1,10); exit 0}
END {exit 1}'
and note that a particular interface can be specified after "ip addr show" if you don't want just the first eth interface. And adapting to ipv6 is a matter of looking for "inet6" instead of "inet"...
Upvotes: 1
Reputation: 1090
The ifdata command (found in the moreutils package) provides an interface to easily retrieve ifconfig data without needing to parse the output from ifconfig manually. It's achieved with a single command:
ifdata -pa eth1
Where eth1
is the name of your network interface.
I don't know how this package behaves when ifconfig is not installed. As Syncrho stated in his answer, ifconfig has been deprecated for sometime, and is no longer found on a lot of modern distributions.
Upvotes: 13
Reputation: 21
In my script i did need only the network part of the IP, so I did it like that
local=$(hostname -I | awk '{print $2}' | cut -f1,2,3 -d".")
Where the cut -f1,2,3 -d"." can be read as "get first 3 parts separated by commas" To change interfaces just change $2 to your interface number, to get whole IP remove cut.
Upvotes: 2
Reputation: 5972
I think the most reliable answer is :
ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | awk -F: '{print $2}' | awk '{print $1}' | head -1
AND
hostname -I | awk -F" " '{print $1}'
because when you don't use head -1
it shows all ips....
Upvotes: 2
Reputation: 33317
man hostname
recommends using the --all-ip-addresses
flag (shorthand -I
), instead of -i
, because -i
works only if the host name can be resolved. So here it is:
hostname -I
And if you are interested only in the primary one, cut
it:
hostname -I | cut -f1 -d' '
Upvotes: 64
Reputation: 2128
I am using
IFACE='eth0'
IP=$(ip -4 address show $IFACE | grep 'inet' | sed 's/.*inet \([0-9\.]\+\).*/\1/')
The advantage of this way is to specify the interface (variable IFACE in the example) in case you are using several interfaces on your host.
Moreover, you could modify ip command in order to adapt this snippet at your convenience (IPv6 address, etc).
Upvotes: 2
Reputation: 1055
I've been struggling with this too until I've found there's a simple command for that purpose
hostname -i
Is that simple!
Upvotes: 77
Reputation: 81
my short version. Useful when you have multiple interface and just want the main ip.
host `hostname` | awk '{print $4}'
Upvotes: 8
Reputation: 246799
You can get just awk to do all the parsing of ifconfig:
ip=$(ifconfig | gawk '
/^[a-z]/ {interface = $1}
interface == "eth0" && match($0, /^.*inet addr:([.0-9]+)/, a) {
print a[1]
exit
}
')
Upvotes: 5
Reputation: 23065
Not really shorter or simpler, but it works for me:
ip=$(ip addr show eth0 | grep -o 'inet [0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | grep -o [0-9].*)
Upvotes: 4