Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / mac2eui64 @ d7841399

History | View | Annotate | Download (1.1 kB)

1
#!/usr/bin/env python
2

    
3
import sys
4
from IPy import IP
5

    
6
if len(sys.argv) != 3:
7
    sys.stderr.write("Usage: %s <mac_address> <IPv6 prefix>\n" % sys.argv[0])
8
    sys.exit(127)
9

    
10
mac = sys.argv[1]
11
try:
12
    prefix = IP(sys.argv[2])
13
except ValueError:
14
    sys.stderr.write("Invalid IPv6 prefix '%s'\n" % sys.argv[2])
15
    sys.exit(1)
16

    
17

    
18
if prefix.version() != 6:
19
    sys.stderr.write("%s is not a valid IPv6 prefix\n" % prefix)
20
    sys.exit(1)
21

    
22
if prefix.prefixlen() != 64:
23
    sys.stderr.write("Cannot generate an EUI-64 address on a non-64 subnet\n")
24
    sys.exit(1)
25

    
26
mac_parts = mac.split(":")
27
pfx_parts = prefix.net().strFullsize().split(":")
28

    
29
if len(mac_parts) != 6:
30
    sys.stderr.write("%s is not a valid MAC-48 address\n" % mac)
31
    sys.exit(1)
32

    
33
eui64 = mac_parts[:3] + [ "ff", "fe" ] + mac_parts[3:]
34

    
35
eui64[0] = "%02x" % (int(eui64[0], 16) ^ 0x02)
36

    
37
ip = ":".join(pfx_parts[:4])
38
for l in range(0, len(eui64), 2):
39
    ip += ":%s" % "".join(eui64[l:l+2])
40

    
41
try:
42
    print IP(ip).strCompressed()
43
except ValueError, e:
44
    sys.stderr.write("Ooops, something went wrong: '%s'!\n" % str(e))
45

    
46
# vim: set ts=4 sts=4 sw=4 et ai :