Statistics
| Branch: | Tag: | Revision:

root / tools / mac2eui64 @ cf51ea5b

History | View | Annotate | Download (1.1 kB)

1
#!/usr/bin/env python
2

    
3
import sys
4

    
5
from IPy import IP
6

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

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

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

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

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

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

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

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

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

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

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