Skip to content

Instantly share code, notes, and snippets.

@danest
Forked from afair/get_mxers.rb
Created October 22, 2017 23:15
Show Gist options
  • Save danest/3d32848eeef7144bdf582e7b3b035dcc to your computer and use it in GitHub Desktop.
Save danest/3d32848eeef7144bdf582e7b3b035dcc to your computer and use it in GitHub Desktop.
Ruby: lookup email MX servers for a domain
require 'resolv'
class Domain
def mxers(domain)
mxs = Resolv::DNS.open do |dns|
ress = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
ress.map { |r| [r.exchange.to_s, IPSocket::getaddress(r.exchange.to_s), r.preference] }
end
return mxs
end
end
p Domain.new.mxers('yahoo.com')
#=> [["mta7.am0.yahoodns.net", "66.94.237.139", 1], ["mta5.am0.yahoodns.net", "67.195.168.230", 1], ["mta6.am0.yahoodns.net", "98.139.54.60", 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment