Skip to content

Instantly share code, notes, and snippets.

@little-apps
Created July 29, 2014 07:02
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 little-apps/88bbd23576008a84e0b6 to your computer and use it in GitHub Desktop.
Save little-apps/88bbd23576008a84e0b6 to your computer and use it in GitHub Desktop.
This script is designed so you can be able to assign many (many) IPv6 addresses to a CentOS 6 machine.
<?php
/*
* Description:
* This script is designed so you can be able to assign many (many) IPv6 addresses to a CentOS 6 machine.
*
* Created By: Little Apps (http://www.little-apps.com)
*
* Steps:
* 1. The first step is to figure out what the IPv6 block is and the gateway for it
* 2. Follow the steps listed at http://www.cyberciti.biz/faq/rhel-redhat-fedora-centos-ipv6-network-configuration/ to setup 1 IPv6 address on your machine
* 3. Set the $ipv6_block to what the IPv6 block is (ie: 2607:f3a0:6:2:24::/80)
* 4. Set $file to the network script for the device (usually /etc/sysconfig/network-scripts/ifcfg-eth0)
* 5. Ensure your root and run this script
* 6. Run "service network restart"
* Notes:
* - This script will make a backup so you can restore the network settings, if necessary
* - This scripts adds IPv6 addresses starting the second IPv6 in the block
* - I'm not responsible for anything that might happen to your machine
*
* License: This script is public domain and doesn't fall under a specific license
*/
/******* ONLY CHANGE THE LINES BELOW *******/
$ipv6_block = '2607:f3a0:6:2:24::/80';
$file = '/etc/sysconfig/network-scripts/ifcfg-eth0';
/******* ONLY CHANGE THE LINES ABOVE *******/
// Get IPv6 and CIDR notation
if (strpos($ipv6_block, '/') === false) {
$start = $ipv6_block;
$cidr = 128;
} else {
list($start, $cidr) = explode('/', $ipv6_block);
if (!is_numeric($cidr))
die('Unable to determine CIDR from string'.PHP_EOL);
if (inet_pton($start) === false)
die('Invalid IPv6 address given.'.PHP_EOL);
$cidr = intval($cidr);
if ($cidr < 1 || $cidr > 128)
die('Invalid CIDR specified (Cannot be less than 1 or more than 128)'.PHP_EOL);
}
// Subtract 1 as its already been added
$ips = (pow(2, (128-$cidr)) - 1);
// Can we write?
if (!is_writable($file))
die('This script does not have permission to write to '.$file.'. Are you running as root?'.PHP_EOL);
// Make backup
if (!copy($file, $file.'.bak'))
die('Unable to make backup of '.$file.'. Stopping...'.PHP_EOL);
file_put_contents($file, PHP_EOL.'IPV6ADDR_SECONDARIES="', FILE_APPEND);
// Increment as the first IP will already be specified in ifcfg-eth0
$current = increment_ip($start, 1);
echo 'Adding IPv6 addresses. Depending on the CIDR, this can take several minutes to complete.'.PHP_EOL;
for ($i = 0; $i < $ips; $i++) {
if ($i == ($ips - 1))
file_put_contents($file, $current . '/64"' . PHP_EOL, FILE_APPEND);
else
file_put_contents($file, $current . '/64 \\' . PHP_EOL, FILE_APPEND);
$current = increment_ip($current, 1);
}
function increment_ip($ip, $increment) {
$addr = inet_pton($ip);
for ($i = strlen($addr) - 1; $increment > 0 && $i >= 0; --$i) {
$val = ord($addr[$i]) + $increment;
$increment = $val / 256;
$addr[$i] = chr($val % 256);
}
return inet_ntop($addr);
}
echo 'Successfully added IPv6 range to '.$file.'!!!'.PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment