#!/usr/bin/env python3

import argparse,os,re,socket,sys

ap=argparse.ArgumentParser(
  usage='usage: %(prog)s [OPTIONS]',
  description='Replace IP addresses in standard input with host names on its way to standard output.',
)
opt=ap.parse_args()

reAddr=re.compile(r'\d+\.\d+\.\d+\.\d+')
host_cache={} # This cache grows without bound, so ... we might need to fix that.

# Once Python 3.9 is reliably found in RHEL distributions, we can decorate the
# host() function with @cache and remove the "manual" caching (using host_cache).

def host(addr):
  '''Return the host name of the given IPv4 address.'''

  if addr in host_cache:
    h=host_cache[addr]
  else:
    try:
      h=socket.gethostbyaddr(addr)
    except:
      h=('unknown '+addr,[],[addr])
    host_cache[addr]=h # Store this result so we don't have to look it up again.
  return h[0]

for line in sys.stdin:
  # Remove the newline character.
  if line and line[-1]=='\n':
    line=line[:-1]
  line=reAddr.sub((lambda m:host(m.group())),line)
  print(line)
