Linux: Get IP / IPv6 (internal/external) on Command Line/Shell - Comment Page: 2
This is quick tip, howto get internal IP address and external IP address on Linux Shell / Command Line. This guide also show, howto make useful Bash functions to get IP addresses quickly.
Note: All functions could be named as you wish and to make functions permanent, add functions to ~/.bashrc or /etc/bashrc. Also all awk commands should work also with gawk and nawk.
[inttf_post_ad1]
1. Get Internal IP Address(es) on Linux Shell / Command Line
1.1 Get IP and IPv6 Address by Interface
Returns plain IP addresses.
/sbin/ifconfig $1 | grep "inet\|inet6" | awk -F' ' '{print $2}' | awk '{print $1}'
## Example usage ##
/sbin/ifconfig eth0 | grep "inet\|inet6" | awk -F'...
Thank You.. Its very usefull..
Or, how about select the interface in the default route:
/sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
Good for scripting against the most likely candidate to be the main IP address, not matter if you are on a machine that uses bonding (bond0) or the more simple, and often used in VMs, single interface setup (eth0).
But how reliable is ipecho.net???
2013-11-07 15:34:43h UTC
[code]
curl http://ipecho.net/plain; echo
!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”
html head
title 503 Service Unavailable /title
body
Service Unavailable
The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.
Apache Server at ipecho.net Port 80
[/code]
Hi J G Miller,
Maybe, there is not any 100% uptime web site/service with this info, so easiest way is do couple fallbacks, if you want to use this on some critical place.
Or if you have own reliable server(s) online, then you can easily return this info.
Hey guys I’m looking for some help. I can not find the external ip address of my Rasberry pi. any ideas on what I can do to find it?
Hi Jim,
Did you tried:
If you have curl installed:
$ curl ifconfig.me
This is the method I use for scripts that need my public ip.
curl http://icanhazip.com/
On recent Debian (Wheezy), you should not match on ‘inet addr’ but ‘inet adr’.
Thanks for the command. On Debian you have to do a grep for “inet adr” instead “inet addr”
ifconfig eth0 | grep "inet adr" | awk -F: '{print $2}' | awk '{print $1}'
If you don’t have lynx or curl (rare, I know…) you may still have wget:
wget -qO- http://ipecho.net/plain
or
wget -q -O – http://ipecho.net/plain
You beat me to it… I was going to suggest
wget -q http://ipecho.net/plain -O – 2>/dev/null ;echo
Try ifcfg.me it supports more protocols:
curl ifcfg.me
nslookup . ifcfg.me
telnet ifcfg.me
ftp ifcfg.me
finger @ifcfg.me
Here’s a shorter yet equally as effective way to get a single IP address by interface:
/sbin/ifconfig $1 | perl -nle’/dr:(\S+)/ && print $1′