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