Skip to content

Instantly share code, notes, and snippets.

@krists
Last active August 29, 2015 14:22
Show Gist options
  • Save krists/7967e6718a9cd611e31a to your computer and use it in GitHub Desktop.
Save krists/7967e6718a9cd611e31a to your computer and use it in GitHub Desktop.
What is my external IP address
#!/usr/bin/env ruby
require "net/http"
require "json"
uri = URI('https://duckduckgo.com/?q=what+is+my+ip&format=json&no_html=1')
response = Net::HTTP.get_response(uri)
if response.is_a?(Net::HTTPSuccess)
parsed_response = JSON.parse(response.body)
answer = parsed_response["Answer"]
ip_regexp = %r/\A.*(\b(?:\d{1,3}\.){3}\d{1,3}\b).*\z/
extract_results = answer.match(ip_regexp)
if extract_results
puts extract_results.captures[0]
else
raise "Could not find IP in answer \"#{answer}\""
end
else
raise "Could not reach DuckDuckGo"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment