Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

MX Record Lookups

by Gerard (Pilgrim)
on Dec 10, 2003 at 22:00 UTC ( [id://313896]=perlquestion: print w/replies, xml ) Need Help??

Gerard has asked for the wisdom of the Perl Monks concerning the following question:

Morning all, good to see you.
I have some nasty code here which I wrote a while back to do MX record lookups. It actually works, but I was just looking at it, and realised it was a bit nasty: there must be a better way. I am still really bad at regular expressions... Here is some code
use Net::DNS::Resolver; use Net::DNS::RR; use Net::DNS::Packet; use strict; #Takes an email address and returns the #mx record (uri of the smtp server that looks after this domain) as a +string sub mxLookup(){ #extract the domain from the email address my ($email) = @_; my @parts = split(/\@/, $email); my $domain = @parts[1]; #Create a DNS Resolver and request records of type MX my $server; my $res = new Net::DNS::Resolver; $res->nameservers("202.27.184.3", "202.27.184.5", "203.96.152.4", " +203.96.152.12"); my $packet = $res->send($domain, "MX") || return "error"; #Parse the results to get the preferred smtp server for this domain my $answer = $packet->string; my $answerSection = ";; ANSWER SECTION"; my @results; @results=split(/$answerSection/, $answer); @results=split(/;;/, $results[1]); @results=split(/\)\n/, $results[0]); @results=split(/\n/, $results[1]); my $priority = "99999999999"; foreach(@results){ my @results2=split(/\t/, $_); my @results3=split(/ /, $results2[4]); if ($results3[0] < $priority){ $server = $results3[1]; } } if (!$server){ return "error"; } return $server; }
Any ideas?
Regards,
Gerard

Replies are listed 'Best First'.
Re: MX Record Lookups
by rob_au (Abbot) on Dec 10, 2003 at 22:06 UTC
    This can be done a lot easier using other Net::DNS::Packet and Net::DNS::RR methods ...

    my $packet = $res->send($domain, 'MX'); foreach my $answer ($packet->answer) { next unless $answer->type eq 'MX'; push @results, $answer->exchange; }

    Additionally, depending upon your requirements, it may be worthwhile to return a list of mail exchanges ($answer->exchange) and their respective preferences ($answer->preference).

     

    perl -le "print+unpack'N',pack'B32','00000000000000000000001010011010'"

      hmmmm.... Why couldn't I find that when I was writing the code. I seem to remember pouring over it for quite some time.
      Thanks for your help
      Regards,
      Gerard
Re: MX Record Lookups
by Zaxo (Archbishop) on Dec 10, 2003 at 22:29 UTC

    The Net::DNS module wraps much of that for you - no need to rewrite all of it. After the Net::DNS docs:

    use Net::DNS; my @mx = mx($domain);
    , minimally, or
    use Net::DNS; my $resolvewr = Net::DNS::Resolver->new(); my @mx = mx $resolver, $domain;
    which allows for error handling and other tricks.

    After Compline,
    Zaxo

Re: MX Record Lookups
by grinder (Bishop) on Dec 11, 2003 at 11:07 UTC
Re: MX Record Lookups
by sunadmn (Curate) on Dec 11, 2003 at 14:30 UTC
    This looks a bit ugly like you said and I would suggest using Net::Nslookup it will return exactly what you need with half of the code. I have just finished a project doing the same and this worked awsome.

    Sunadmn
    USE PERL

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://313896]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found