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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.