Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NateBrune/0ea9cc8ce750c6108d12 to your computer and use it in GitHub Desktop.
Save NateBrune/0ea9cc8ce750c6108d12 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import sys
try:
import dns.resolver
except:
print 'dnspython is required'
print 'http://www.dnspython.org/'
sys.exit(0)
def scan_ips(ip):
'''Given an IPv4 prefix, launch a DNS query to each IP address.'''
args = globals()['args']
resolver = dns.resolver.Resolver()
resolver.lifetime = args.timeout
target = args.url or 'google.com'
resolver.nameservers = [ip]
# If we get an answer, it's open
try:
answers = resolver.query(target, 'A')
print '%s,open' % (ip.rstrip())
# NoAnswer: Contacted a server but didn't get a valid response
# NoNameservers: Couldn't get a valid answer from any of the nameservers
# These probably mean it's closed
except (dns.resolver.NoAnswer, dns.resolver.NoNameservers), ex:
print '%s,closed' % (ip.rstrip())
# No response
except dns.resolver.Timeout, ex:
print '%s,unresponsive' % (ip.rstrip())
pass
if __name__ == '__main__':
# Process command line options
import argparse
parser = argparse.ArgumentParser(description = 'Scan IPv6 file for DNS resolvers')
parser.add_argument('-t', '--timeout', type = float, default = 1.0, help = 'timeout for each request')
parser.add_argument('-u', '--url', default = 'google.com', help = 'hostname to use as target')
parser.add_argument('file', type = str, help = 'list of ip addresses')
args = parser.parse_args()
# Run scans, die nicely on Ctrl-C
try:
with open(args.file, "r") as ipsfile:
for ips in ipsfile:
scan_ips(ips)
except KeyboardInterrupt, ex:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment