手撸了一个根据ip段枚举ip地址的功能,配合搜集到的ip段将想要的ip先列出来,目前只支持192.168.1.1-193.168.1.1这样的方式,类似于192.168-190.1.1-125和192.168.1.1/20这样的后期慢慢写
#coding=utf-8
''''''
import re
import nmap
def EnumerateIp(StartIp,EndIp):
'''枚举Ip,目前只支持192.168.1.1-193.168.1.1这样的方式'''
if IpCheck(StartIp) == 'Yes' and IpCheck(EndIp) == 'Yes':
StartIpValue = IpToDecimal(StartIp)
EndIpValue = IpToDecimal(EndIp)
if EndIpValue >= StartIpValue:
for ip in range(StartIpValue, EndIpValue+1):
print(DecimalToIp(ip))
else:
for ip in range(EndIpValue, StartIpValue+1):
print(DecimalToIp(ip))
def DecimalToIp(ip):
'''将十进制转换为ip'''
IpValue = str((((ip//256)//256)//256))+'.'+str(((ip//256)//256)%256)+'.'+str((ip//256)%256)+'.'+str(ip%256)
return IpValue
def IpToDecimal(hostIp):
'''将ip返回为十进制'''
IpList = hostIp.split('.')
IpValueA = int(IpList[0])
IpValueB = int(IpList[1])
IpValueC = int(IpList[2])
IpValueD = int(IpList[3])
return((((IpValueA*256+IpValueB)*256)+IpValueC)*256+IpValueD)
def IpCheck(hostIp):
'''以.号分割,分成4份,每一份有1-3位,组成为0-9的数字,且数字大于等于0小于等于255'''
try:
pat = re.compile(r'([0-9]{1,3})\.')
r = re.findall(pat,hostIp+".")
if len(r)==4 and len([x for x in r if int(x)>=0 and int(x)<=255])==4:
return('Yes')
except Exception as identifier:
return(identifier)
if __name__ == "__main__":
StartIp = '202.38.135.0'
EndIp = '202.38.134.0'
EnumerateIp(StartIp,EndIp)
|