Link to home
Start Free TrialLog in
Avatar of faithless1
faithless1

asked on

SED Email Validation

Hi,

I have a large list of emails and I need a script that will validate the syntax, MX and DNS records. I run Ubuntu through a virtual box and ideally need a script I can execute on the command line.

Thank you in advance
Avatar of apresence
apresence

This would be a bit of a pain to do in SED.  Here's how to do it in perl:
perl -ne 'exit 1 if (!/^\w[\w\-\_\.]*\@\w[\w\-\_]*(\.\w[\w\-\_]*)+$/)'

Returns 0 if the e-mail address is valid, or 1 if it's not.

Testing:
root@beta:~/exex $ echo foo | perl -ne 'exit 1 if (!/^\w[\w\-\_\.]*\@\w[\w\-\_]*(\.\w[\w\-\_]*)+$/)'; echo $?
1
root@beta:~/exex $ echo foo@bar | perl -ne 'exit 1 if (!/^\w[\w\-\_\.]*\@\w[\w\-\_]*(\.\w[\w\-\_]*)+$/)'; echo $?
1
root@beta:~/exex $ echo foo@bar.com | perl -ne 'exit 1 if (!/^\w[\w\-\_\.]*\@\w[\w\-\_]*(\.\w[\w\-\_]*)+$/)'; echo $?
0
root@beta:~/exex $ echo 0oo@bar.com | perl -ne 'exit 1 if (!/^\w[\w\-\_\.]*\@\w[\w\-\_]*(\.\w[\w\-\_]*)+$/)'; echo $?
0
root@beta:~/exex $ echo 0oo@bar.com | perl -ne 'exit 1 if (!/^\w[\w\-\_\.]*\@\w[\w\-\_]*(\.\w[\w\-\_]*)+$/)'; echo $?
0
root@beta:~/exex $


If you want to go a step further and check for .com/.net/.org, etc. do this (Make sure EVERY domain suffix you want to allow is listed!  .biz and .tv etc...)
perl -ne 'exit 1 if (!/^\w[\w\-\_\.]*\@\w[\w\-\_]*(\.\w[\w\-\_]*)*(\.com|\.net|\.org)$/)'

Testing:
root@beta:~/exex $ echo foo@bar.com | perl -ne 'exit 1 if (!/^\w[\w\-\_\.]*\@\w[\w\-\_]*(\.\w[\w\-\_]*)*(\.com|\.net|\.org)$/)'; echo $?
0
root@beta:~/exex $ echo foo@bar.foo | perl -ne 'exit 1 if (!/^\w[\w\-\_\.]*\@\w[\w\-\_]*(\.\w[\w\-\_]*)*(\.com|\.net|\.org)$/)'; echo $?
1
The above just validates the format of the e-mail address, not the MX/DNS records... please ignore.
Check this out for validating the MX/DNS records (again, perl not SED):
http://www.usenix.org/publications/perl/perl17.html

SED is really only suited for editing/validating text, not for doing things like querying domain servers.  Perl is a much better option for this.
        sub valid_address {
        	my($addr) = @_;
        	my($domain, $valid);
         	return(0) unless ($addr =~ /^[^@]+@([-\w]+\.)+[A-Za-z]
        					{2,4}$/);
        	$domain = (split(/@/, $addr))[1];
        	$valid = 0; open(DNS, "nslookup -q=any $domain |") ||
        					return(-1);
        	while (<DNS>) {
        		$valid = 1 if (/^$domain.*\s(mail exchanger|
        					internet address)\s=/);
        	}
        	return($valid);
        }

Open in new window

Avatar of faithless1

ASKER

Thanks. How would I run these scripts on the command line in Gnome terminal? I have a file with 100k emails named (emails.txt). Thanks
Drop the attached code into a file called validate_emails.pl.  Make sure to run "chmod 700 validate_emails.pl" to mark it as executable.

To process your text file and see the output, just use:
validate_emails.pl < emails.txt

To process your text file and save the output, just use:
validate_emails.pl < emails.txt > output.txt

Sample testing output:
root@beta:~/exex/test13 $ cat emails.txt
foo
foo@bar
foo@bar.baz
support@microsoft.com
root@beta:~/exex/test13 $ ./validate_emails.pl <emails.txt
foo invalid
foo@bar invalid
foo@bar.baz invalid
support@microsoft.com valid
root@beta:~/exex/test13 $
#!/usr/bin/perl

sub valid_address {
  my($addr) = @_;
  my($domain, $valid);
  return(0) unless ($addr =~ /^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/);
  $domain = (split(/@/, $addr))[1];
  $valid = 0; open(DNS, "nslookup -q=any $domain |") || return(-1);
  while (<DNS>) {
    $valid = 1 if (/^$domain.*\s(mail exchanger|internet address)\s=/);
  }
  return($valid);
}

while (<>) {
  $addy = $_;
  $addy =~ s/\s+$//;
  if ($addy)
  {
    print "$addy " . (valid_address($addy) ? 'valid' : 'invalid') . "\n";
  }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of apresence
apresence

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Superb, thanks a million! I appreciate it.