#!/usr/bin/env python import string,sys def validhost(host): if host[1] == 0: return(0)# Port is 0 if host[0][0:3] == '10.': return(0)# Private IP elif host[0][0:3] == '192': return(0)# Private IP elif host[0][0:3] == '172': # Must do more testing... if int(string.split(host[0], '.')[1]) in range(16, 32): return(0)# Private IP elif host[0][0:2] == '0.': return(0)# Reserved IP elif host[0][0:2] == '1.': return(0)# Reserved IP elif host[0][0:2] == '2.': return(0)# Reserved IP elif host[0][0:3] == '127': return(0)# Loopback IP if string.split(host[0], '.')[3] == '0': return(0)# Invalid IP if '255' in string.split(host[0], '.'): return(0)# Broadcast IP # Host seems valid. return(1) def pongify(host): rawipaddress = '' for i in string.split(host[0], '.'): rawipaddress = rawipaddress + chr(int(i)) rawport = chr(host[1] % 256) + chr(host[1] / 256) rawnonguidheader = '\001\000\000\016\000\000\000' host = rawnonguidheader + rawport + rawipaddress + 8*'\000' return(host) def main(): #f = open('.gnut_hosts', 'r') # From. #t = open('pytella.hosts.binary', 'w') # To. f = sys.stdin # From. t = sys.stdout # To. while 1: host = string.strip(f.readline()) if host == '': break # We're done, yay! host = string.split(host) host = (host[0], int(host[1])) if validhost(host): t.write(pongify(host)) #t.write(host[0]+' '+str(host[1])+'\n') #print "Added host:", `host` #else: #pass #print "Did not add host:", `host` f.close() t.close() main() # EOF