Skip to content

Instantly share code, notes, and snippets.

@Chaz6
Last active December 18, 2021 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chaz6/0af1271092b52c74f2c67c7a9cb0745a to your computer and use it in GitHub Desktop.
Save Chaz6/0af1271092b52c74f2c67c7a9cb0745a to your computer and use it in GitHub Desktop.
Generate a hosts file using DNS64
www.example.com 93.184.216.34
#!/usr/bin/env perl
use strict;
use Net::IP qw(ip_is_ipv4 ip_is_ipv6 ip_compress_address);
my $domains_file = 'domains.txt';
my $prefixes_file = 'prefixes.txt';
sub convert_ip_to_hex {
my $prefix = shift;
my $addr = shift;
my ($a,$b,$c,$d) = split(/\./, $addr);
$a = sprintf("%02x", $a);
$b = sprintf("%02x", $b);
$c = sprintf("%02x", $c);
$d = sprintf("%02x", $d);
my $ip = $a.$b.":".$c.$d;
my $ip1 = new Net::IP('::'.$ip);
my $ip2 = new Net::IP($prefix);
my $ip3 = $ip1->binadd($ip2);
return ip_compress_address($ip3->ip(),6);
}
my %domains;
open my $fh, '<', $domains_file or die;
while (my $row = <$fh>) {
chomp $row;
my ($domain, $ip) = split(/ /,$row);
push(@{$domains{$domain}},$ip);
}
close $fh;
my @prefixes;
open $fh, '<', $prefixes_file or die;
while (my $row = <$fh>) {
chomp $row;
push(@prefixes,$row);
}
close $fh;
foreach my $domain (keys %domains) {
foreach my $ip (@{$domains{$domain}}){
foreach my $prefix (@prefixes) {
if(ip_is_ipv6($prefix) && ip_is_ipv4($ip)) {
print convert_ip_to_hex($prefix,$ip)." ".$domain."\n";
}
}
}
}
2a0b:f4c0:4d:1::
2a0b:f4c0:4d:2::
2a0b:f4c0:4d:3::
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment