Public IP is used for communication between computers over the Internet. A computer running with public IP is accessible all over the world using the Internet. So we can say that it is the identity of the computer on the internet. Now the question is how do we know our public IP?. For computers having GUI can easily get there IP using web tools like this but how to get public IP of the computers having terminal access only. The solution is here – use one of the following commands to find public IP of your system using Linux terminal. These are also useful to use in a shell script.
Find Public IP using Linux Command
Command 1 –
Use dig command to find your public IP address. The dig command is a DNS lookup utility for Linux systems to look up your public IP address by connecting to the OpenDNS servers.
dig +short myip.opendns.com @resolver1.opendns.com
Command 2 –
Use wget command to get your Public IP address as below example.
wget http://ipecho.net/plain -O - -q ; echo
Command 3,4,5 –
Use curl command to get your Public address.
curl ipecho.net/plain; echo
curl icanhazip.com
curl ifconfig.me
Get Public IP in Shell Script
We can simply use following commands in our shell script get our computers public IP and store them in a variable to use anywhere in a shell script.
1 2 3 4 | #!/bin/bash PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo` echo $PUBLIC_IP |
8 Comments
Thank you Rahul, and everyone who added sites. I put seven sites in a bash function. The last one gives more info. The first few times I ran this, opendns reported a different IP. I have helped someone whose public IP was diverted, and her logins were hacked in realtime so it was bad to log in anywhere until the method was discoverred. https://pastebin.com/DiPUdqPJ
myip() {
( for site in ipaddr.pub/cli ipecho.net/plain icanhazip.com ifconfig.me \
ipconfig.in/ip diagnostic.opendns.com/myip
do echo “$site ”
wget -qO- $site
echo
done ) | sed -n -E ‘/^$/d;H;${g;s/^[\n]+//;s/( )\n/ /g;p;}’
wget -qO- ipinfo.io | sed ‘1s/.*/ipinfo.io:/;$d;’
}
also add, curl ipconfig.in/ip
or visit http://www.ipconfig.in
Amazing I have added them in my note, I have got the same ip after checking them at http://whatsmyrouterip.com/ so cool
Nice, but, you can also in simple way hit this:
$ curl ipinfo.io
As simple as it!
No need for faraday. Use the standard library
require ‘open-uri’
open(‘http://icanhazip.com/’).read.chomp
made a small ruby script which pick a random service url from the 4 you provided in your blog post
#!/usr/bin/env ruby
require ‘faraday’
# http://tecadmin.net/5-commands-to-get-public-ip-using-linux-terminal/
urls = %w[
http://ipecho.net/plain
http://observebox.com/ip
http://icanhazip.com
http://ifconfig.me
]
url = urls[(rand * urls.size).to_i]
STDERR.puts “I will be using #{url} to find out your external IP”
puts Faraday.get(url).body
Thanks, great your are providing 5 nice urls like this..
here a use in Ruby
require ‘faraday’
Faraday.get(‘http://icanhazip.com/’).body.chomp
Thanks dude. Added to favorites.