Created
February 12, 2018 15:59
-
-
Save cromulus/9015b083999c1aec2abf1df4d14248c4 to your computer and use it in GitHub Desktop.
Ruby DNS TTL
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
def get_ip(hostname) | |
dns = Resolv.new | |
redis = Redis.new # storing in redis | |
ip = redis.get("ip:#{hostname}") | |
return ip unless ip.nil? | |
begin | |
resource = dns.getresource(hostname, Resolv::DNS::Resource::IN::A) | |
rescue Resolv::ResolvError | |
return false | |
end | |
# storing in redis for only as long as the TTL allows | |
redis.setex("ip:#{hostname}", resource.address.ttl, resource.address.to_s) | |
resource.address.to_s # IP address as string | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment