Skip to content

Instantly share code, notes, and snippets.

@chew-z
Created February 28, 2017 09:58
Show Gist options
  • Save chew-z/2bfae0d48feed4dd3e342cb05e396d82 to your computer and use it in GitHub Desktop.
Save chew-z/2bfae0d48feed4dd3e342cb05e396d82 to your computer and use it in GitHub Desktop.
filters dnscrypt-resolvers.csv file
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use Text::CSV;
my $csv = Text::CSV->new({ sep_char => ',' });
my $outfile = 'resolvers.csv';
open(my $fh, '>', $outfile) or die "Cannot create $outfile, $!\n";
my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
my $linenumber = 0;
while (my $line = <$data>) {
chomp $line;
$linenumber += 1;
if ($linenumber < 2) {
print "$linenumber: Resolver address\n";
print $fh "$line\n";
} elsif ($csv->parse($line)) {
my @fields = $csv->fields();
# NOT IP6 AND (ends in :53, :443, :2053 ,:5353 OR ends like IP4)
if ($fields[10] !~ m/^\[/ and ($fields[10] =~ m/:443$|:2053$|:5353$|:53$/ or $fields[10] =~ m/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) {
print "$linenumber: $fields[10]\n";
print $fh "$line\n"
}
} else {
# be quiet
}
}
close $fh or die "Error in closing the file ", __FILE__, " $!\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment