summaryrefslogtreecommitdiff
path: root/netaddr/tests/2.x/eui/tutorial.txt
blob: 00e031553c8549bb1138f57ab81b2bd952dd1e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
First of all you need to pull the various MAC related classes and functions into your namespace.

.. note:: Do this for the purpose of this tutorial only. In your own code, you should be explicit about the classes, functions and constants you import to avoid name clashes.

>>> from netaddr import *

You can reasonably safely import everything from the netaddr namespace as care has been taken to only export the necessary classes, functions and constants. 

Always hand pick your imports if you are unsure about possible name clashes.

----------------
Basic operations
----------------

Instances of the EUI class are used to represent MAC addresses.

>>> mac = EUI('00-1B-77-49-54-FD')

Standard repr() access returns a Python statement that can reconstruct the MAC address object from scratch if executed in the Python interpreter.

>>> mac
EUI('00-1B-77-49-54-FD')

Accessing the EUI object in the string context.

>>> str(mac)
'00-1B-77-49-54-FD'
>>> '%s' % mac
'00-1B-77-49-54-FD'

Here are a few other common properties.

>>> str(mac), str(mac.oui), mac.ei, mac.version
('00-1B-77-49-54-FD', '00-1B-77', '49-54-FD', 48)

-------------------------
Numerical representations
-------------------------

You can view an individual IP address in various other formats.

>>> int(mac) == 117965411581
True
>>> hex(mac)
'0x1b774954fd'
>>> oct(mac)
'01556722252375'
>>> mac.bits()
'00000000-00011011-01110111-01001001-01010100-11111101'
>>> mac.bin
'0b1101101110111010010010101010011111101'

----------
Formatting
----------

It is very common to see MAC address in many different formats other than the standard IEEE EUI-48.

The EUI class constructor handles all these common forms.

>>> EUI('00-1B-77-49-54-FD')
EUI('00-1B-77-49-54-FD')

IEEE EUI-48 lowercase format

>>> EUI('00-1b-77-49-54-fd')
EUI('00-1B-77-49-54-FD')

Common UNIX format

>>> EUI('0:1b:77:49:54:fd')
EUI('00-1B-77-49-54-FD')

Cisco triple hextet format

>>> EUI('001b:7749:54fd')
EUI('00-1B-77-49-54-FD')
>>> EUI('1b:7749:54fd')
EUI('00-1B-77-49-54-FD')
>>> EUI('1B:7749:54FD')
EUI('00-1B-77-49-54-FD')

Bare MAC addresses (no delimiters)

>>> EUI('001b774954fd')
EUI('00-1B-77-49-54-FD')
>>> EUI('01B774954FD')
EUI('00-1B-77-49-54-FD')

PostreSQL format (found in documentation)

>>> EUI('001B77:4954FD')
EUI('00-1B-77-49-54-FD')

It is equally possible to specify a selected format for your MAC string output in the form of a 'dialect' class. It's use is similar to the dialect class used in the Python standard library csv module.

>>> mac = EUI('00-1B-77-49-54-FD')
>>> mac
EUI('00-1B-77-49-54-FD')
>>> mac.dialect = mac_unix
>>> mac
EUI('0:1b:77:49:54:fd')
>>> mac.dialect = mac_cisco
>>> mac
EUI('001b.7749.54fd')
>>> mac.dialect = mac_bare
>>> mac
EUI('001B774954FD')
>>> mac.dialect = mac_pgsql
>>> mac
EUI('001b77:4954fd')

You can of course, create your own dialect classes to customise the MAC formatting if the standard ones do not suit your needs.

Here's a tweaked UNIX MAC dialect that generates uppercase, zero-filled octets.

>>> class mac_custom(mac_unix): pass
>>> mac_custom.word_fmt = '%.2X'
>>> mac = EUI('00-1B-77-49-54-FD', dialect=mac_custom)
>>> mac
EUI('00:1B:77:49:54:FD')

-----------------------------------
Querying organisational information
-----------------------------------

EUI objects provide an interface to the OUI (Organisationally Unique Identifier) and IAB (Individual Address Block) registration databases available from the IEEE.

Here is how you query an OUI with the EUI interface.

>>> mac = EUI('00-1B-77-49-54-FD')
>>> oui = mac.oui
>>> oui
OUI('00-1B-77')
>>> oui.registration().address
['Lot 8, Jalan Hi-Tech 2/3', 'Kulim Hi-Tech Park', 'Kulim Kedah 09000', 'MALAYSIA']
>>> oui.registration().org
'Intel Corporate'

You can also use OUI objects directly without going through the EUI interface.

A few OUI records have multiple registrations against them. I'm not sure if this is recording historical information or just a quirk of the IEEE reigstration process.

This example show you how you access them individually by specifying an index number.

>>> oui = OUI(524336)   #   OUI constructor accepts integer values too.
>>> oui
OUI('08-00-30')
>>> oui.registration(0).address
['2380 N. ROSE AVENUE', 'OXNARD CA 93010', 'UNITED STATES']
>>> oui.registration(0).org
'NETWORK RESEARCH CORPORATION'
>>> oui.registration(0).oui
'08-00-30'
>>> oui.registration(1).address
['CH-1211 GENEVE 23', 'SUISSE/SWITZ', 'SWITZERLAND']
>>> oui.registration(1).org
'CERN'
>>> oui.registration(1).oui
'08-00-30'
>>> oui.registration(2).address
['GPO BOX 2476V', 'MELBOURNE VIC 3001', 'AUSTRALIA']
>>> oui.registration(2).org
'ROYAL MELBOURNE INST OF TECH'
>>> oui.registration(2).oui
'08-00-30'
>>> for i in range(oui.reg_count):
...     str(oui), oui.registration(i).org
...
('08-00-30', 'NETWORK RESEARCH CORPORATION')
('08-00-30', 'CERN')
('08-00-30', 'ROYAL MELBOURNE INST OF TECH')

Here is how you query an IAB with the EUI interface.

>>> mac = EUI('00-50-C2-00-0F-01')
>>> mac.is_iab()
True
>>> iab = mac.iab
>>> iab
IAB('00-50-C2-00-00-00')
>>> iab.registration()
{'address': ['1241 Superieor Ave E', 'Cleveland OH 44114', 'UNITED STATES'],
 'iab': '00-50-C2-00-00-00',
 ...
 'offset': 80,
 'org': 'T.L.S. Corp.',
 'size': 139}