Created
August 9, 2012 06:44
-
-
Save yunazuno/3301748 to your computer and use it in GitHub Desktop.
change ipv6 address of specified interface(not tested)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import subprocess | |
import binascii | |
import os | |
interface = "tap0" | |
## Get current ipv6 address | |
cmd = "ifconfig {0}|grep -e 'inet6.*global'|awk '{{print $2}}'" | |
cmd = cmd.format(interface) | |
addr_old = subprocess.check_output(cmd, shell=True).strip() | |
addr_prefix = ':'.join(addr_old.split(':')[:4]) + ':' | |
## Generate new ipv6 address | |
# get 4 * 16bit pseudo-random number by hex-string | |
byte2int = lambda n: int(binascii.hexlify(n), 16) | |
random_hex = ["{0:x}".format(byte2int(os.urandom(16 / 8))) | |
for i in xrange(4)] | |
# make new ipv6 address | |
addr_new = addr_prefix + ':'.join(random_hex) | |
## Set new address and delete old one | |
# set | |
cmd = "ifconfig {0} add {1}/64".format(interface, addr_new) | |
print cmd | |
subprocess.call(cmd, shell=True) | |
# delete | |
cmd = "ifconfig {0} del {1}/64".format(interface, addr_old) | |
print cmd | |
subprocess.call(cmd, shell=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment