rendered paste bodyimport sys
import re
ip_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.']
ip_format = "([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)"
def main():
argv = sys.argv
argc = len(argv)
current_num = ''
ip_string = ''
number_of_ips = 0
if argc != 2:
print "Error! Correct usage is \"ip_convert file.txt\"."
filename = argv[1]
try: file = open(filename, 'r')
except IOError:
print "Error: Could not find file " + filename + "."
return 0
x = file.read()
file.close()
ip_string = ''
file = open("output.txt", 'w') #here we open it to delete any old output file, and create an empty one
file.close()
file = open("output.txt", 'wa')
for i in range(len(x)):
if x[i] in ip_chars:
ip_string += x[i]
if (i == len(x)-1) or (x[i] not in ip_chars):
if(check_ip_validity(ip_string)):
if number_of_ips == 0:
file.write(ip_string)
else:
file.write(', ' + ip_string)
number_of_ips += 1
ip_string = ''
file.close()
print "finished."
print "found", number_of_ips, "ips"
def check_ip_validity(ip_string):
if re.match(ip_format , ip_string):
x = re.findall(ip_format, ip_string)
if x and len(x[0]) != 4:
return False
for i in x[0]:
if not 0 <= int(i) <= 255:
return False
else:
return False
if len(re.findall("\.", ip_string)) == 3:
return True
else:
return False
if __name__ == "__main__":
main()