Answer the question
In order to leave comments, you need to log in
How can the code be simplified?
import requests
f = open('ip.txt','r')
f1 = open('white_list.txt','w')
ip_list = f.read().split('\n') # xxx.xxx.xxx.xxx-xxx.xxx.xxx.xxx
for ip in ip_list:
ip1, ip2 = ip.split('-')
ip0 = ".".join(ip1.split('.')[:3]) + '.'
print(ip2)
num1 = int(ip1.split('.')[-1])
num2 = int(ip2.split('.')[-1]) + 1
for i in range(num1, num2):
try:
r = requests.get('http://' + ip0 + str(i) + ':8000', timeout=2)
print('http://' + ip0 + str(i) + ':8000')
f1.write('http://' + ip0 + str(i) + ':8000\n')
except:
continue
f.close()
f1.close()
Answer the question
In order to leave comments, you need to log in
Sketched on the knee:
https://gist.github.com/igorzakhar/bbd054c1f1290dd...
Or:
import asyncio
import re
import aiohttp
def load_ip_list(filename):
with open(filename, 'r') as fp:
ip_list = fp.read().rstrip().split('\n')
return ip_list
def save_file(filename, responses):
with open(filename, 'w') as fp:
for response in responses:
if response:
url, status = response
save_line = "{:28} {}\n".format(url, status)
fp.write(save_line)
def get_ipaddresses_range(ip_list):
ip_addresses_range = []
for ip_addr in ip_list:
pattern = r'([\d\.]+\.)(\d+)-[\d\.]+\.(\d+)$'
network, host_min, host_max = re.search(pattern, ip_addr).groups()
ip_range = [
network + str(host)
for host in range(int(host_min), int(host_max) + 1)
]
ip_addresses_range.extend(ip_range)
return ip_addresses_range
def create_urls_list(ip_addr_range):
url_scheme = 'http'
port = '8000'
url_pattern = "{}://{}:{}"
urls = [
url_pattern.format(url_scheme, ip, port)
for ip in ip_addr_range
]
return urls
async def get_response_status(url, session):
try:
async with session.head(url, timeout=2) as response:
return url, response.status
except asyncio.TimeoutError:
pass
async def check_ip(urls_list, session):
tasks = [
asyncio.ensure_future(get_response_status(url, session))
for url in urls_list
]
responses = await asyncio.gather(*tasks)
return responses
async def run(loop, urls_list):
async with aiohttp.ClientSession(loop=loop) as session:
results = await check_ip(urls_list, session)
return results
if __name__ == '__main__':
ip_list = load_ip_list('ip.txt')
ip_range = get_ipaddresses_range(ip_list)
urls = create_urls_list(ip_range)
loop = asyncio.get_event_loop()
results = loop.run_until_complete(run(loop, urls))
save_file('white_list.txt', results)
loop.close()
For "parsing" text it is convenient to use re (regex)
>>> re.search(r'([\d\.]+\.)(\d+)-[\d\.]+\.(\d+)$', '127.0.0.10-127.0.0.50').groups()
('127.0.0.', '10', '50')
if you are trying to make your own port scanner - I advise you to look towards nmap
if you want something cooler for pentesting - kali
if you want to write well in python - read
from personal feelings:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question