Skip to content

Instantly share code, notes, and snippets.

@cromulus
Created February 12, 2018 15:59
Show Gist options
  • Save cromulus/9015b083999c1aec2abf1df4d14248c4 to your computer and use it in GitHub Desktop.
Save cromulus/9015b083999c1aec2abf1df4d14248c4 to your computer and use it in GitHub Desktop.
Ruby DNS TTL
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