Home > Learning > 列举两个合法ip之间的所有有效ip

列举两个合法ip之间的所有有效ip

想获得两个ip之间所有的有效ip,用来检测各种漏洞。

ip纯真数据库只提供ip段的头和尾,想完整测试某机构的所有ip就要获取头尾ip之间的所有ip,但是网上竟然没找到有用的,好不容易找到一个被转发好多次的,竟然是搞两个for循环糊弄人,索性自己写:

用法:python ips_between_two_ip.py ip1 ip2

只要ip2在ip1后面即可

#
# author:Leviathan
# date:2017/4/5
#
import struct
import socket
import sys
import os
def findIPs(start, end):
 if os.path.isfile("ips_from_"+start+"_to_"+end+".txt")==True:
 os.remove("ips_from_"+start+"_to_"+end+".txt")
 ips=open("ips_from_"+start+"_to_"+end+".txt","a")
 ipstruct = struct.Struct('>I')
 start, = ipstruct.unpack(socket.inet_aton(start))
 end, = ipstruct.unpack(socket.inet_aton(end))
 print "[+]start to list each valid ip od your range."
 for i in range(start, end+1):
 ip=socket.inet_ntoa(ipstruct.pack(i))
 ip_array=ip.split('.')
 if int(ip_array[3])==0 or int(ip_array[3])==255:
 pass
 else:
 ips.write(ip+'\n')
 ips.close()

def check_legality(ips1,ips2):
 ips1_array=ips1.split('.')
 ips2_array=ips2.split('.')
 try:
 if len(ips1_array)==4 and len(ips2_array)==4:
 res_flag=0
 for i in range(0,4,1):
 if int(ips1_array[i])>=0 and int(ips2_array[i])>=0 and int(ips1_array[i])<255 and int(ips2_array[i])<255:
 res_flag=1
 continue
 else:
 res_flag=0
 break
 if res_flag==1:
 print "[+]ip is legal."
 findIPs(ips1,ips2)
 elif res_flag==0:
 print "[-]ip is ILLEGAL."
 return False
 except Exception,e:
 print "[-]ip is ILLEGAL:type."
 print "[-]"+e

try:
 ip1=sys.argv[1]
 ip2=sys.argv[2]
 check_legality(ip1,ip2)
except:
 if len(sys.argv)!=3:
 print "[-]Something's wrong with your input."
 print "[!]Example:python x.py 10.0.0.1 10.0.0.2"

You may alo like...

(2) Comments

  1. 匿名

    看了兄弟的BLOG,妈蛋,wordpress 前端比我做好的,python写的比我好,树莓派玩的比我溜,渗透也可以。scrapy玩的好,最后,兄弟,交个朋友不?

    1. Cao

      66666

发表评论

邮箱地址不会被公开。 必填项已用*标注