Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an iterator of octet quadruples instead of building a list of string... #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions pingrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,39 @@
# python pingrange.py 10.1.0-1.0-255 will ping 10.1.0.0/23
#
#
import itertools
import re
try:
from cli import cli
from cli import cli
except ImportError:
from cisco import cli
from cisco import cli
from argparse import ArgumentParser

def expandrange(rangefunc):
hosts = []
octets = rangefunc.split('.')
for i,octet in enumerate(octets):
if '-' in octet:
octetrange = octet.split('-')
for digit in range(int(octetrange[0]), int(octetrange[1])+1):
ip = '.'.join(octets[:i] + [str(digit)] + octets[i+1:])
hosts += expandrange(ip)
break
else:
hosts.append(rangefunc)
return hosts

def parse_octet_ranges(octet_ranges_spec):
"""
Parses IP range spec into four iterables of numbers corresponding to each
octet's range.
"""
octet_ranges = [
[int(n) for n in octet_range.split('-')]
for octet_range in octet_ranges_spec.split('.')
]
return [
octet_range if len(octet_range) == 1
else range(octet_range[0], octet_range[1] + 1)
for octet_range in octet_ranges
]


parser = ArgumentParser('pingrange')
parser.add_argument('ip', help='IP range to ping, e.g., 10.1.0-1.0-255 will expand to 10.1.0.0/23')
parser.add_argument('options', nargs='*', help='Options to pass to ping', default=['count 1'])
args = parser.parse_args()
targets = expandrange(args.ip)
a_range, b_range, c_range, d_range = parse_octet_ranges(args.ip)

for ip in targets:
for a, b, c, d in itertools.product(a_range, b_range, c_range, d_range):
ip = '%d.%d.%d.%d' % (a, b, c, d)
m = re.search('([0-9\.]+)% packet loss', cli('ping %s %s' % (ip, ' '.join(args.options))))
print('%s - %s' % (ip, 'UP' if float(m.group(1)) == 0.0 else 'DOWN'))