Skip to content

Instantly share code, notes, and snippets.

@Fedcomp
Created October 24, 2016 13:22
Show Gist options
  • Save Fedcomp/26330d54b195a5a1e3c904961ccb4b95 to your computer and use it in GitHub Desktop.
Save Fedcomp/26330d54b195a5a1e3c904961ccb4b95 to your computer and use it in GitHub Desktop.
IPV6 generator
<?php
$filename = "ip.list";
$prefix = "2a04:5200:8";
function generate_ipv6($prefix){
function generate_ipv6_block() {
$seed = str_split('1234567890abcdef');
shuffle($seed);
$block = join("", $seed); // Symbol array to string
$block = substr($block, 0, 4);
return $block;
}
$a = generate_ipv6_block();
$b = generate_ipv6_block();
$c = generate_ipv6_block();
$d = generate_ipv6_block();
$e = generate_ipv6_block();
return "{$prefix}:{$a}:{$b}:{$c}:{$d}:{$e}";
}
function generate_unique_ipv6($prefix, $iplist) {
$ipv6 = null; // Scope
do {
$ipv6 = generate_ipv6($prefix);
} while(in_array($ipv6, $iplist));
return $ipv6;
}
if(file_exists($filename)){
$ips = trim(file_get_contents($filename));
$ips = explode("\n", $ips);
}
else{
$ips = [];
}
$ip = generate_unique_ipv6($prefix, $ips);
$ips[] = $ip;
file_put_contents($filename, implode("\n", $ips));
echo $ip;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment