0
# Network Objects
1
2
Comprehensive classes for handling IPv4/IPv6 addresses, network calculations, interface name parsing, and Layer 4 protocol information. These objects provide vendor-agnostic network element representation with specialized support for Cisco and Juniper naming conventions.
3
4
## Capabilities
5
6
### IPv4 Address Handling
7
8
Complete IPv4 address and network manipulation with subnet calculations and address classification.
9
10
```python { .api }
11
class IPv4Obj:
12
def __init__(self, ipv4_str):
13
"""
14
Initialize IPv4 address object.
15
16
Parameters:
17
- ipv4_str (str): IPv4 address string (e.g., '192.168.1.1/24', '10.0.0.0 255.255.255.0')
18
"""
19
20
@property
21
def ip(self):
22
"""
23
Get the IPv4 address object.
24
25
Returns:
26
IPv4Address: The IP address component
27
"""
28
29
@property
30
def network(self):
31
"""
32
Get the IPv4 network object.
33
34
Returns:
35
IPv4Network: The network component
36
"""
37
38
@property
39
def netmask(self):
40
"""
41
Get the network mask as dotted decimal string.
42
43
Returns:
44
str: Netmask in dotted decimal format (e.g., '255.255.255.0')
45
"""
46
47
@property
48
def prefixlen(self):
49
"""
50
Get the network prefix length.
51
52
Returns:
53
int: CIDR prefix length (e.g., 24)
54
"""
55
56
@property
57
def network_object(self):
58
"""
59
Get the IPv4Network object for this address.
60
61
Returns:
62
IPv4Network: Network object containing this address
63
"""
64
65
@property
66
def hostmask(self):
67
"""
68
Get the host mask (inverse of netmask).
69
70
Returns:
71
str: Host mask in dotted decimal format
72
"""
73
74
@property
75
def compressed(self):
76
"""
77
Get compressed string representation.
78
79
Returns:
80
str: Compressed IPv4 address string
81
"""
82
83
@property
84
def exploded(self):
85
"""
86
Get exploded string representation.
87
88
Returns:
89
str: Exploded IPv4 address string
90
"""
91
92
@property
93
def packed(self):
94
"""
95
Get packed binary representation.
96
97
Returns:
98
bytes: 4-byte packed binary representation
99
"""
100
101
def is_private(self):
102
"""
103
Check if address is in private address space.
104
105
Returns:
106
bool: True if private address (RFC 1918)
107
"""
108
109
def is_multicast(self):
110
"""
111
Check if address is multicast.
112
113
Returns:
114
bool: True if multicast address (224.0.0.0/4)
115
"""
116
117
def is_reserved(self):
118
"""
119
Check if address is reserved.
120
121
Returns:
122
bool: True if reserved address space
123
"""
124
```
125
126
### IPv6 Address Handling
127
128
Complete IPv6 address and network manipulation with modern IPv6 features and address classification.
129
130
```python { .api }
131
class IPv6Obj:
132
def __init__(self, ipv6_str):
133
"""
134
Initialize IPv6 address object.
135
136
Parameters:
137
- ipv6_str (str): IPv6 address string (e.g., '2001:db8::1/64')
138
"""
139
140
@property
141
def ip(self):
142
"""
143
Get the IPv6 address object.
144
145
Returns:
146
IPv6Address: The IPv6 address component
147
"""
148
149
@property
150
def network(self):
151
"""
152
Get the IPv6 network object.
153
154
Returns:
155
IPv6Network: The IPv6 network component
156
"""
157
158
@property
159
def prefixlen(self):
160
"""
161
Get the network prefix length.
162
163
Returns:
164
int: IPv6 prefix length (e.g., 64)
165
"""
166
167
@property
168
def compressed(self):
169
"""
170
Get compressed IPv6 string representation.
171
172
Returns:
173
str: Compressed IPv6 address (e.g., '2001:db8::1')
174
"""
175
176
@property
177
def exploded(self):
178
"""
179
Get exploded IPv6 string representation.
180
181
Returns:
182
str: Exploded IPv6 address with all segments
183
"""
184
185
@property
186
def packed(self):
187
"""
188
Get packed binary representation.
189
190
Returns:
191
bytes: 16-byte packed binary representation
192
"""
193
194
def is_private(self):
195
"""
196
Check if address is in private address space.
197
198
Returns:
199
bool: True if private IPv6 address
200
"""
201
202
def is_multicast(self):
203
"""
204
Check if address is multicast.
205
206
Returns:
207
bool: True if multicast address (ff00::/8)
208
"""
209
210
def is_reserved(self):
211
"""
212
Check if address is reserved.
213
214
Returns:
215
bool: True if reserved IPv6 address space
216
"""
217
```
218
219
### Interface Name Parsing
220
221
Parse and manipulate network interface names with vendor-specific support and normalization.
222
223
```python { .api }
224
class CiscoIOSInterface:
225
def __init__(self, interface):
226
"""
227
Initialize Cisco IOS interface name parser.
228
229
Parameters:
230
- interface (str): Interface name (e.g., 'GigabitEthernet0/1', 'Gi0/1')
231
"""
232
233
@property
234
def name(self):
235
"""
236
Get the full interface name.
237
238
Returns:
239
str: Complete interface name (e.g., 'GigabitEthernet0/1')
240
"""
241
242
@property
243
def port(self):
244
"""
245
Get the port number.
246
247
Returns:
248
str: Port identifier (e.g., '0/1')
249
"""
250
251
@property
252
def port_type(self):
253
"""
254
Get the interface type.
255
256
Returns:
257
str: Interface type (e.g., 'GigabitEthernet')
258
"""
259
260
@property
261
def subinterface(self):
262
"""
263
Get the subinterface number if present.
264
265
Returns:
266
int: Subinterface number or None
267
"""
268
269
@property
270
def channel(self):
271
"""
272
Get the channel number for Port-Channel interfaces.
273
274
Returns:
275
int: Channel number or None
276
"""
277
278
def abbreviate(self):
279
"""
280
Get abbreviated interface name.
281
282
Returns:
283
str: Abbreviated name (e.g., 'Gi0/1')
284
"""
285
286
class CiscoIOSXRInterface:
287
def __init__(self, interface):
288
"""
289
Initialize Cisco IOS-XR interface name parser.
290
291
Parameters:
292
- interface (str): IOS-XR interface name (e.g., 'GigabitEthernet0/0/0/1', 'TenGigE0/1/0/0')
293
"""
294
295
@property
296
def name(self):
297
"""
298
Get the full interface name.
299
300
Returns:
301
str: Complete interface name (e.g., 'GigabitEthernet0/0/0/1')
302
"""
303
304
@property
305
def port(self):
306
"""
307
Get the port number portion.
308
309
Returns:
310
str: Port identifier (e.g., '0/0/0/1')
311
"""
312
313
@property
314
def port_type(self):
315
"""
316
Get the interface type.
317
318
Returns:
319
str: Interface type (e.g., 'GigabitEthernet', 'TenGigE', 'Bundle-Ether')
320
"""
321
322
def abbreviate(self):
323
"""
324
Get abbreviated interface name format.
325
326
Returns:
327
str: Abbreviated interface name (e.g., 'Gi0/0/0/1' for 'GigabitEthernet0/0/0/1')
328
"""
329
```
330
331
### Range Handling
332
333
Handle Cisco-style numeric ranges with compression and expansion capabilities.
334
335
```python { .api }
336
class CiscoRange:
337
def __init__(self, text="", result_type=int):
338
"""
339
Initialize Cisco range handler.
340
341
Parameters:
342
- text (str): Range specification (e.g., '1-5,7,9-12')
343
- result_type (type): Type for range elements (int, str)
344
"""
345
346
def append(self, val):
347
"""
348
Add value to the range.
349
350
Parameters:
351
- val: Value to add to range
352
"""
353
354
def remove(self, val):
355
"""
356
Remove value from the range.
357
358
Parameters:
359
- val: Value to remove from range
360
"""
361
362
@property
363
def compressed_str(self):
364
"""
365
Get compressed string representation.
366
367
Returns:
368
str: Compressed range string (e.g., '1-5,7,9-12')
369
"""
370
371
def as_list(self):
372
"""
373
Get range as expanded list.
374
375
Returns:
376
list: All values in the range as list
377
"""
378
```
379
380
### Layer 4 Protocol Objects
381
382
Represent Layer 4 protocol and port information for access control and service definitions.
383
384
```python { .api }
385
class L4Object:
386
def __init__(self, protocol="", port_spec="", syntax=""):
387
"""
388
Initialize Layer 4 protocol object.
389
390
Parameters:
391
- protocol (str): Protocol name or number (e.g., 'tcp', 'udp', '6')
392
- port_spec (str): Port specification (e.g., '80', '1-1024', 'www')
393
- syntax (str): Configuration syntax for port parsing
394
"""
395
396
@property
397
def protocol(self):
398
"""
399
Get the Layer 4 protocol.
400
401
Returns:
402
str: Protocol name or number
403
"""
404
405
@property
406
def port_list(self):
407
"""
408
Get list of port numbers.
409
410
Returns:
411
list[int]: List of individual port numbers
412
"""
413
414
@property
415
def port_spec(self):
416
"""
417
Get the port specification string.
418
419
Returns:
420
str: Original port specification
421
"""
422
```
423
424
### DNS Response Objects
425
426
Handle DNS query responses with comprehensive record type support.
427
428
```python { .api }
429
class DNSResponse:
430
"""
431
DNS query response object.
432
433
Attributes:
434
- query_name (str): The queried domain name
435
- query_type (str): DNS record type (A, AAAA, MX, etc.)
436
- response (str): DNS response data
437
- preference (int): MX record preference value
438
- rdtype (str): Resource record type
439
"""
440
441
@property
442
def query_name(self):
443
"""Get the queried domain name."""
444
445
@property
446
def query_type(self):
447
"""Get the DNS record type."""
448
449
@property
450
def response(self):
451
"""Get the DNS response data."""
452
453
@property
454
def preference(self):
455
"""Get MX record preference."""
456
457
@property
458
def rdtype(self):
459
"""Get resource record type."""
460
```
461
462
## Utility Functions
463
464
### IP Address Factory
465
466
Create appropriate IP address objects automatically based on input format.
467
468
```python { .api }
469
def ip_factory(addr):
470
"""
471
Create IPv4Obj or IPv6Obj based on address format.
472
473
Parameters:
474
- addr (str): IP address string
475
476
Returns:
477
IPv4Obj|IPv6Obj: Appropriate IP address object
478
"""
479
```
480
481
### Address Operations
482
483
Perform operations on collections of IP addresses.
484
485
```python { .api }
486
def collapse_addresses(addresses):
487
"""
488
Collapse overlapping address ranges into summarized networks.
489
490
Parameters:
491
- addresses (list): List of IP address/network objects
492
493
Returns:
494
list: Collapsed address ranges
495
"""
496
497
def check_valid_ipaddress(addr):
498
"""
499
Validate IP address format.
500
501
Parameters:
502
- addr (str): IP address string to validate
503
504
Returns:
505
bool: True if valid IP address format
506
"""
507
```
508
509
## Usage Examples
510
511
### IPv4 Address Manipulation
512
513
```python
514
from ciscoconfparse import IPv4Obj
515
516
# Create IPv4 object from CIDR notation
517
ip = IPv4Obj('192.168.1.10/24')
518
519
print(f"IP Address: {ip.ip}") # 192.168.1.10
520
print(f"Network: {ip.network}") # 192.168.1.0/24
521
print(f"Netmask: {ip.netmask}") # 255.255.255.0
522
print(f"Prefix Length: {ip.prefixlen}") # 24
523
print(f"Is Private: {ip.is_private()}") # True
524
525
# Create from IP and netmask
526
ip2 = IPv4Obj('10.0.0.1 255.255.0.0')
527
print(f"Network: {ip2.network}") # 10.0.0.0/16
528
```
529
530
### IPv6 Address Handling
531
532
```python
533
from ciscoconfparse import IPv6Obj
534
535
# Create IPv6 object
536
ipv6 = IPv6Obj('2001:db8:85a3::8a2e:370:7334/64')
537
538
print(f"Compressed: {ipv6.compressed}") # 2001:db8:85a3::8a2e:370:7334
539
print(f"Network: {ipv6.network}") # 2001:db8:85a3::/64
540
print(f"Prefix Length: {ipv6.prefixlen}") # 64
541
print(f"Is Private: {ipv6.is_private()}") # False
542
```
543
544
### Interface Name Parsing
545
546
```python
547
from ciscoconfparse import CiscoIOSInterface
548
549
# Parse interface names
550
intf1 = CiscoIOSInterface('GigabitEthernet0/1')
551
print(f"Full Name: {intf1.name}") # GigabitEthernet0/1
552
print(f"Port: {intf1.port}") # 0/1
553
print(f"Type: {intf1.port_type}") # GigabitEthernet
554
print(f"Abbreviated: {intf1.abbreviate()}") # Gi0/1
555
556
# Handle subinterfaces
557
intf2 = CiscoIOSInterface('FastEthernet1/0.100')
558
print(f"Subinterface: {intf2.subinterface}") # 100
559
```
560
561
### Range Operations
562
563
```python
564
from ciscoconfparse import CiscoRange
565
566
# Create and manipulate ranges
567
vlan_range = CiscoRange('1-10,15,20-25')
568
print(f"As List: {vlan_range.as_list()}") # [1, 2, 3, ..., 10, 15, 20, 21, ..., 25]
569
570
vlan_range.append(30)
571
vlan_range.remove(5)
572
print(f"Compressed: {vlan_range.compressed_str}") # 1-4,6-10,15,20-25,30
573
```
574
575
### Layer 4 Protocol Handling
576
577
```python
578
from ciscoconfparse import L4Object
579
580
# Define protocol and port specifications
581
web_service = L4Object('tcp', '80,443')
582
print(f"Protocol: {web_service.protocol}") # tcp
583
print(f"Ports: {web_service.port_list}") # [80, 443]
584
585
# Handle port ranges
586
mail_service = L4Object('tcp', '25,110,143,993-995')
587
print(f"Port Spec: {mail_service.port_spec}") # 25,110,143,993-995
588
```
589
590
### IP Address Factory
591
592
```python
593
from ciscoconfparse import ip_factory
594
595
# Automatically create appropriate IP objects
596
ipv4_obj = ip_factory('192.168.1.1/24') # Returns IPv4Obj
597
ipv6_obj = ip_factory('2001:db8::1/64') # Returns IPv6Obj
598
599
print(type(ipv4_obj).__name__) # IPv4Obj
600
print(type(ipv6_obj).__name__) # IPv6Obj
601
```