Is there a way to run a program or script on a linux box to see what MX records are used by default when sending outgoing email from mail
or mailx
? I'm writing a utility that is going to be run on a large, random set of linux machines, and I'd like that utility to automatically figure out the MX hosts that will actually be used on each machine when mail
or mailx
are used.
I know I can look at the machine's mail server's config file to see what the setting is for outgoing email, but each host might be using a different email server (sendmail
, postfix
, exim
, etc.), and I don't want to write a utility that searches for each one and then tries to parse whatever config files it might find.
If I knew the algorithm that mail
and mailx
use for deciding what server to use for outgoing mail, I could then replicate that algorithm within my own utility.
Do mail
and mailx
just try 127.0.0.1
? Or do they do something like getting the current host name and then using a DNS query to find the MX records for that host, or if none exist, to use the info from the A record? Or do they simply use the sendmail
program (in which case, I'd need to figure out which outgoing server that sendmail
chooses)? ... or what?
Thank you very much.
I have found the source code for mail
and for a version of mailx
, and it seems like mail
just uses the sendmail
executable, and mailx
allows an optional specification of an SMTP server, or if that's absent, it also uses the sendmail
executable.
This doesn't help me much, and so I think I need to use my own algorithm. Here's what I have come up with (pseudo-code). Does anyone see any problems with this? ...
set `mxs` = list of IP addr(s) of current hostname's MX record(s)
if `mxs` cannot be determined
set `mxs` = one-element list of IP addr of current hostname's A record
if `mxs` cannot be determined
set `mxs` = one-element list of current host's IP address
if for some reason `mxs` still can't be determined
set `mxs` = one-element list containing '127.0.0.1'
end if
end if
end if
In the end, mxs
will contain a list of IP addresses upon which the current machine's SMTP server is likely to be listening. It's still possible that none of these will actually work for sending email, but that's OK for my purposes.
In the real world, I'll use a set rather than a list, to avoid duplicate entries.
How does this look?