Ruby
Ruby Examples
Ruby’s standard library handles this just fine. No gems needed.
Get Your Own IP
require "net/http"
require "json"
uri = URI("https://whatismyip.technology/api/me")
response = Net::HTTP.get(uri)
data = JSON.parse(response)
puts "Your IP: #{data['ip']}"
Full IP Lookup
require "net/http"
require "json"
uri = URI("https://whatismyip.technology/api/8.8.8.8")
response = Net::HTTP.get(uri)
data = JSON.parse(response)
puts "IP: #{data['ip']}"
puts "City: #{data['city']}"
puts "Country: #{data['country']}"
puts "ISP: #{data.dig('asn', 'name')}"
Geolocation Only
uri = URI("https://whatismyip.technology/api/8.8.8.8/geo")
geo = JSON.parse(Net::HTTP.get(uri))
puts "#{geo['city']}, #{geo['region']}, #{geo['country']}"
ASN Info
uri = URI("https://whatismyip.technology/api/8.8.8.8/asn")
asn = JSON.parse(Net::HTTP.get(uri))
puts "ASN #{asn['number']}: #{asn['name']} (#{asn['org']})"
Privacy Check
uri = URI("https://whatismyip.technology/api/8.8.8.8/privacy")
privacy = JSON.parse(Net::HTTP.get(uri))
puts "VPN: #{privacy['is_vpn']}"
puts "Proxy: #{privacy['is_proxy']}"
puts "Tor: #{privacy['is_tor']}"
Bulk Lookup
require "net/http"
require "json"
uri = URI("https://whatismyip.technology/api/bulk")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri)
request["Content-Type"] = "application/json"
request.body = { ips: ["8.8.8.8", "1.1.1.1", "208.67.222.222"] }.to_json
response = http.request(request)
results = JSON.parse(response.body)
results.each do |r|
puts "#{r['ip']} -> #{r['country']}"
end
With Error Handling
require "net/http"
require "json"
def lookup_ip(ip)
uri = URI("https://whatismyip.technology/api/#{ip}")
response = Net::HTTP.get_response(uri)
unless response.is_a?(Net::HTTPSuccess)
puts "Error: HTTP #{response.code}"
return nil
end
JSON.parse(response.body)
rescue StandardError => e
puts "Request failed: #{e.message}"
nil
end
data = lookup_ip("8.8.8.8")
if data
puts "#{data['ip']} is in #{data['city']}, #{data['country']}"
end
Complete Script
#!/usr/bin/env ruby
require "net/http"
require "json"
API_BASE = "https://whatismyip.technology/api/me"
ip = ARGV[0]
url = ip ? "#{API_BASE}/#{ip}" : API_BASE
begin
response = Net::HTTP.get(URI(url))
data = JSON.parse(response)
puts JSON.pretty_generate(data)
rescue => e
$stderr.puts "Error: #{e.message}"
exit 1
end
Save as iplookup.rb and run with ruby iplookup.rb 8.8.8.8 or just ruby iplookup.rb for your own IP.