Advertisement
Guest User

Check your HOSTS File!

a guest
Oct 25th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Copyright: ME!
  4. # License: The latest version of GPL as posted on the EFF website at the time
  5. #          you copy this file.  As of 2008, that is GPLv3.
  6.  
  7. import re, sys
  8. from collections import defaultdict
  9.  
  10. SE = sys.stderr
  11.  
  12. "DOES NOT STRICTLY CHECK FOR SIMILARITIES LIKE 127.1 vs 127.0.0.1"
  13.  
  14. comment_re = re.compile( r'^[ \t]*#.*$' )
  15. valid_re   = re.compile( r'^(\d+\S+)[ \t]+(\w.*)+$' )
  16. alias_re   = re.compile( r'^\w+(\.\w+)*$' )
  17.  
  18. alias_mapping = defaultdict( list )
  19.  
  20. #for noLine, line in enumerate( sys.stdin ):
  21. for noLine, line in enumerate( open('/tmp/hosts', 'rb') ):
  22.     noLine += 1   # Humans think from 1, apparently.
  23.  
  24.     # To catch variances in frickin' whitespace.  Fix your damn text editors,
  25.     # people, and show your EOL whitespace!
  26.     line = line.rstrip('\n')
  27.  
  28.     if not line:
  29.         continue
  30.  
  31.     if comment_re.match( line ):
  32.         continue
  33.  
  34.     match = valid_re.match( line )
  35.     if match:
  36.         ip4, aliases = match.groups()
  37.  
  38.         ip4 = ip4.split('.')
  39.         if len( ip4 ) > 4:
  40.             msg = "Line {}: Bad IPv4 address '{}'\n"
  41.             SE.write( msg.format( noLine, ip4 ))
  42.         else:
  43.             for i in ip4:
  44.                 try:
  45.                     i = int( i );
  46.                     if not ( 0 <= i and i < 255 ):
  47.                         msg = "Line {}: Bad IPv4 address '{}'\n"
  48.                         SE.write( msg.format( noLine, ip4 ))
  49.                         break
  50.                 except:
  51.                     msg = "Line {}: Bad IPv4 address '{}'\n"
  52.                     SE.write( msg.format( noLine, ip4 ))
  53.                     break
  54.  
  55.         aliases = aliases.split()
  56.         for alias in aliases:
  57.             if not alias_re.match( alias ):
  58.                 msg = "Line {}: Invalid host alias name '{}'\n"
  59.                 SE.write( msg.format( noLine, alias ))
  60.  
  61.             elif alias.lower() != alias:
  62.                 msg = ('Line {}: Please use company standard lower case domain '
  63.                   "name in alias '{}'\n")
  64.                 SE.write( msg.format( noLine, alias ))
  65.  
  66.             alias_mapping[ alias ].append( '.'.join(ip4) )
  67.  
  68.         continue
  69.  
  70.     SE.write( "Line {}: Invalid syntax\n".format( noLine ))
  71.  
  72.  
  73. for alias, ip_addresses in sorted( alias_mapping.iteritems() ):
  74.     if len( ip_addresses ) > 1:
  75.         if len( set( ip_addresses )) == 1:
  76.             msg = "Multiple definitions of alias '{}' to IP '{}'\n"
  77.             SE.write( msg.format( alias, ip_addresses[0] ))
  78.         else:
  79.             msg = "Multiple IPs assigned to alias '{}': {}\n"
  80.             SE.write( msg.format( alias, ', '.join( ip_addresses )))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement