扩展IP范围

时间:2019-12-22 04:46:24

标签: python

我希望这个脚本能正常工作。我有一个具有IP的csv文件。一些IP的格式为1.2.3.4-1.2.3.9。我希望脚本将该范围扩展为单个IP地址,如下所示:

1.2.3.4
1.2.3.5
1.2.3.6
1.2.3.7
1.2.3.8
1.2.3.9

我有以下脚本,但它在多个位置都出现缩进错误

(python test_expand.py
  File "test_expand.py", line 5
    item=""
       ^
IndentationError: expected an indented block) 

以下代码:


   class qualys_expand_ip:  
  def __init__(self):  
  item=""  

  def expand_ip(self,item):  
  item = item.strip()  
  dash = item.find("-")  
  if dash > 0:  
  ip=item.split("-")  
  first_ip=str(ip[0])  
  second_ip=str(ip[1])  

  #first ip  
  first_octet_1 = item.find(".", 0, len(first_ip))   
  second_octet_1 = item.find(".",first_octet_1+1, len(first_ip))  
  third_octet_1 = item.find(".", second_octet_1+1, len(first_ip))   
  fourth_octet_1 = item.rfind(".", 0, len(first_ip))   



  #second ip  
  first_octet_2 = item.find(".", 0, len(second_ip))  
  second_octet_2 = item.find(".", first_octet_2+1, len(second_ip))  
  third_octet_2 = item.find(".", second_octet_2+1, len(second_ip))  
  fourth_octet_2 = item.rfind(".", 0, len(second_ip))  


  if first_ip[0:first_octet_1] == second_ip[0:first_octet_2]:  
  ##print first_ip[0:first_octet_1]  

  if first_ip[first_octet_1+1:second_octet_1] == second_ip[first_octet_2+1:second_octet_2]:  
  ##print first_ip[first_octet_1+1:second_octet_1]  

  if first_ip[second_octet_1+1:third_octet_1] == second_ip[second_octet_2+1:third_octet_2]:  
  ##print first_ip[second_octet_1+1:third_octet_1]  

  range_1= first_ip[fourth_octet_1 + 1:len(first_ip)]  
  range_2= second_ip[fourth_octet_2 + 1:len(second_ip)]  


  returnip=[]  
  for i in range(int(range_1),int(range_2)+1):  
  returnip.append(first_ip[0:first_octet_1]+"."+first_ip[first_octet_1+1:second_octet_1]+"."+first_ip[second_octet_1+1:third_octet_1]+"."+str(i))  
  return returnip  
  else:  
  range_21= first_ip[second_octet_1+1:third_octet_1]  
  range_22= second_ip[second_octet_2+1:third_octet_2]  



  returnip=[]  
  for x in range(int(range_21),int(range_22)+1):  
  range_1= first_ip[fourth_octet_1 + 1:len(first_ip)]  
  range_2= second_ip[fourth_octet_2 + 1:len(second_ip)]  

  if x == int(range_22):  
  for i in range(0,int(range_2)+1):  
  returnip.append(first_ip[0:first_octet_1]+"."+first_ip[first_octet_1+1:second_octet_1]+"."+str(x)+"."+str(i))  
  else:  
  for i in range(int(range_1),255+1):  
  returnip.append(first_ip[0:first_octet_1]+"."+first_ip[first_octet_1+1:second_octet_1]+"."+str(x)+"."+str(i))  
  return returnip  


  else:  
  returnip =["error1"]  
  return returnip  
  else:  
  returnip =["error2"]  
  return returnip  
  else:  
  ## If it has 3 dots, it's an IP address  
  if item.count(".") == 3:  
  item=[item]  
  return item  
  else:  
  ip_range = item + ".0 - " + item + ".255"  
  returnip=[]  
  for ip in range(0,256):  
  returnip.append(item+"."+str(ip))  
  return returnip  




## MAIN  
addr_sum = 0  
file_in = open('IP.csv', 'r')  
file_out = open('singleline.txt', 'w')  


expander = qualys_expand_ip()  
for item in file_in:  
        list_ip=expander.expand_ip(item)  
        for ip in list_ip:  
                addr_sum = addr_sum+1  
                file_out.write(ip + "\n")  
print (addr_sum)  
file_in.close()  
file_out.close() 

1 个答案:

答案 0 :(得分:1)

Python具有内置的ipaddress导入,您可以使用:

import ipaddress

ip = ipaddress.ip_address('1.2.3.4')
for x in range(0,5):
   ip = ip + 1
   print(ip)

输出:

1.2.3.5
1.2.3.6
1.2.3.7
1.2.3.8
1.2.3.9