Created
June 13, 2012 19:03
-
-
Save janpipek/2925817 to your computer and use it in GitHub Desktop.
tell your current public IP using ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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