netwatch.py/netaddr-0.7.10/netaddr/tests/2.x/ip/nmap.txt

115 lines
2.5 KiB
Plaintext
Raw Normal View History

2015-03-27 22:54:25 +00:00
=nmap IP Range Tests=
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
{{{
>>> from netaddr import *
}}}
nmap IP range validation.
{{{
>>> valid_nmap_range('192.0.2.1')
True
>>> valid_nmap_range('192.0.2.0-31')
True
>>> valid_nmap_range('192.0.2-3.1-254')
True
>>> valid_nmap_range('0-255.0-255.0-255.0-255')
True
>>> valid_nmap_range('192.168.3-5,7.1')
True
>>> valid_nmap_range('192.168.3-5,7,10-12,13,14.1')
True
>>> valid_nmap_range(1)
False
>>> valid_nmap_range('1')
False
>>> valid_nmap_range([])
False
>>> valid_nmap_range({})
False
>>> valid_nmap_range('::')
False
>>> valid_nmap_range('255.255.255.256')
False
>>> valid_nmap_range('0-255.0-255.0-255.0-256')
False
>>> valid_nmap_range('0-255.0-255.0-255.-1-0')
False
>>> valid_nmap_range('0-255.0-255.0-255.256-0')
False
>>> valid_nmap_range('0-255.0-255.0-255.255-0')
False
>>> valid_nmap_range('a.b.c.d-e')
False
>>> valid_nmap_range('255.255.255.a-b')
False
}}}
nmap IP range iteration.
{{{
>>> list(iter_nmap_range('192.0.2.1'))
[IPAddress('192.0.2.1')]
>>> ip_list = list(iter_nmap_range('192.0.2.0-31'))
>>> len(ip_list)
32
>>> ip_list
[IPAddress('192.0.2.0'), IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), IPAddress('192.0.2.15'), IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), IPAddress('192.0.2.18'), IPAddress('192.0.2.19'), IPAddress('192.0.2.20'), IPAddress('192.0.2.21'), IPAddress('192.0.2.22'), IPAddress('192.0.2.23'), IPAddress('192.0.2.24'), IPAddress('192.0.2.25'), IPAddress('192.0.2.26'), IPAddress('192.0.2.27'), IPAddress('192.0.2.28'), IPAddress('192.0.2.29'), IPAddress('192.0.2.30'), IPAddress('192.0.2.31')]
>>> ip_list = list(iter_nmap_range('192.0.2-3.1-7'))
>>> len(ip_list)
14
>>> list(iter_nmap_range('192.0.2.1-3,5,7-9'))
[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.5'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9')]
>>> for ip in ip_list:
... print ip
...
192.0.2.1
192.0.2.2
192.0.2.3
192.0.2.4
192.0.2.5
192.0.2.6
192.0.2.7
192.0.3.1
192.0.3.2
192.0.3.3
192.0.3.4
192.0.3.5
192.0.3.6
192.0.3.7
>>> list(iter_nmap_range('::'))
Traceback (most recent call last):
...
AddrFormatError: invalid nmap range: ::
}}}