Skip to content

Instantly share code, notes, and snippets.

@janpipek
Created June 13, 2012 19:03
Show Gist options
  • Save janpipek/2925817 to your computer and use it in GitHub Desktop.
Save janpipek/2925817 to your computer and use it in GitHub Desktop.
tell your current public IP using ruby
#!/usr/bin/ruby
# This script prints your current public IP
#
# It retrieves it using http://checkip.dyndns.org web page.
# If given a parameter, it treats it as (float) number of seconds
# before timeout. The default timeout is set to 10 seconds.
#
# Author: Jan Pipek (jan.pipek AT gmail.com)
require 'net/http'
require 'timeout'
# Maximum number of seconds to wait (as float)
secs_to_wait = ARGV[0].to_f || 10
begin
Timeout::timeout(secs_to_wait) do
response = Net::HTTP.get URI.parse('http://checkip.dyndns.org')
puts response.match( /(?:Address: )([\d\.]+)/ )[1]
end
rescue
puts "Timeout: Unable to reach checkip.dyndns.org."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment