Skip to content

Instantly share code, notes, and snippets.

@colszowka
Last active October 12, 2022 10:39
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save colszowka/3a59633d54b6adb3e278 to your computer and use it in GitHub Desktop.
Save colszowka/3a59633d54b6adb3e278 to your computer and use it in GitHub Desktop.
Ruby DNS Check
require 'resolv'
class DnsCheck
attr_reader :host
def initialize(host)
@host = host
end
def a
@a ||= Resolv::DNS.new.getresources(host, Resolv::DNS::Resource::IN::A)
end
def a?
a.any?
end
def mx
@mx ||= Resolv::DNS.new.getresources(host, Resolv::DNS::Resource::IN::MX)
end
def mx?
mx.any?
end
def ns
@ns ||= Resolv::DNS.new.getresources(host, Resolv::DNS::Resource::IN::NS)
end
def ns?
ns.any?
end
end
require 'testrocket'
require './dns_check'
!-> { 'It returns true / record sets for legit domains' }
+-> { DnsCheck.new('github.com').a? }
+-> { DnsCheck.new('github.com').a.any? }
+-> { DnsCheck.new('github.com').mx? }
+-> { DnsCheck.new('github.com').mx.any? }
+-> { DnsCheck.new('github.com').ns? }
+-> { DnsCheck.new('github.com').ns.any? }
!-> { 'It returns false / empty arrays for invalid domains' }
--> { DnsCheck.new('boomthisisinvalid.com').a? }
+-> { DnsCheck.new('boomthisisinvalid.com').a.empty? }
--> { DnsCheck.new('boomthisisinvalid.com').mx? }
+-> { DnsCheck.new('boomthisisinvalid.com').mx.empty? }
--> { DnsCheck.new('boomthisisinvalid.com').ns? }
+-> { DnsCheck.new('boomthisisinvalid.com').ns.empty? }
--> { DnsCheck.new('not even a domain 💩').a? }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment