#!/s/bin/python -- # -*- python -*- # $Id: nigh,v 1.2 1998-11-10 10:40:50-06 annis Exp $ # $Source: /home/orion/annis/code/nigh/RCS/nigh,v $ # # Copyright (c) 1998 William S. Annis. All rights reserved. This # is free software; you can redistribute it and/or modify it under the # same terms as Perl (the Artistic Licence). """Look for addresses in the neighborhood. This program is to help you get some idea who an address's neighbors are. This is most relevent when an address does not appear to have been registered with a DNS name. The default behavior for nigh is to stop once five named neighbors have been found. The '-a' option forces nigh to scan the entire class-C subnet and '-f #' will let you set the cut-off to something else; '-f 5', then, is the default. Addresses are searched in this order: from the given address, down ten, up ten, down the remaining addresses, then up the remaining addresses, quitting when the specified max has been found. Multiple address may be specified, in which case the -a and -f options apply for all addresses given. nigh 144.92.141.55 nigh -f 15 128.104.10.98 nigh -a 144.92.141.55 128.103.1.22 """ __author__ = "William S. Annis" __rcs_version__ = '$Revision: 1.2 $' __version__ = '0.1' import socket, string, sys, getopt # Try to find this address. Return 0 if there isn't one. def getaddr(addr): try: addrs = socket.gethostbyaddr(addr) first_name, addresses, ip = addrs return first_name except: return 0 # Chop up our IP address. Returns a tuple (domain, net), i.e. # domain_and_net('144.92.141.45') ==> ('144.92.141', '45') def domain_and_net(addr): (a, b, c, d) = string.split(addr, ".") return (string.join([a, b, c], "."), d) # Now, the work. found = 0 # How many matches so far. maxfound = 5 # Quit after how many matches. # Parse command line. try: (args, address_list) = getopt.getopt(sys.argv[1:], "af:h") except: print "Usage: %s [ -a ] [ -f # ] { IP-address }*" % sys.argv[0] print "Or '-h' for help." sys.exit(1) for arg in args: if arg[0] == '-a': maxfound = 255 if arg[0] == '-f': maxfound = string.atoi(arg[1]) if arg[0] == '-h': print __doc__ # Hit each address on the line. for address in address_list: found = 0 (domain, net) = domain_and_net(address) # Generate the search list (arguments to pass to range via apply) nnet = string.atoi(net) if nnet < 11: first = (nnet, 0, -1) else: first = (nnet, nnet - 10, -1) if nnet > 245: second = (nnet, 255, 1) else: second = (nnet, nnet + 10, 1) third = (first[1], 0, -1) fourth = (second[1], 255, 1) # Now, do the lookups. domain = domain + "." print "Domain: ", domain for group in (first, second, third, fourth): if found > maxfound: break # Percolate up to 'for address ...' for addr in apply(range, group): address = domain + `addr` name = getaddr(address) if name: print "%s: %s" % (address, name) found = found + 1 if found > maxfound: break # EOF