I'm trying to use just the IP address (inet) as a parameter in a script I wrote.
Is there an easy way in a unix terminal to get just the IP address, rather than looking through ifconfig
?
I'm trying to use just the IP address (inet) as a parameter in a script I wrote.
Is there an easy way in a unix terminal to get just the IP address, rather than looking through ifconfig
?
On Redhat 64bit, this solved problem for me.
Generally, it is never guaranteed that a system will only have one IP address, for example, you can have both an ethernet and wlan connections, and if you have an active VPN connection then you'll have yet another IP address.
Linux
On Linux,
hostname -I
will list the current IP address(es). Relying on it always returning just one IP address will most likely not work as expected under some scenarios (i.e. a VPN link is up), so a more reliable way would be converting the result to an array and then loop over the elements:OSX
On OSX, if you know the interface, you could use:
which will return just the IP address.
Or you could loop over possible interface names, starting with a suffix, i.e.
en
:Also, getting the IP address becomes non-deterministic in case both a cable and wifi connections are established, when a machine has more than one ethernet interfaces, or when VPN tunnels are present.
Getting the external IP
If you need the external IP, then you can query a text-mode service, for example
curl ipecho.net/plain
would return a plain text external IP.Use the following command: