P
P
pepelac02018-08-11 00:27:46
Python
pepelac0, 2018-08-11 00:27:46

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()

The code is simple. The given lists of ip - addresses are served . xxx.xxx.xxx.10 - xxx.xxx.xxx.50
And you just need to check if you can go to xxx.xxx.xxx.xxx:8000.
Should requests be replaced with sockets? Multithreading? How to parse the value 10 and 50 from strings? Is there an easier way out?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
igorzakhar, 2018-08-13
@igorzakhar

Sketched on the knee:
https://gist.github.com/igorzakhar/bbd054c1f1290dd...
Or:

Example code using aiohttp
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()

L
lega, 2018-08-11
@lega

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')

For "massive" asyncio network tasks - you can send 10k requests at a time, instead of checking in a loop.

A
Artyom Innokentiev, 2018-08-14
@artinnok

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:

  • the code is very complicated for a simple task
  • you will not be able to understand this code in 3-6 months
  • socket is optional in this situation, requests is enough
  • at this stage, multithreading is redundant for this code
  • read about variable naming in python
  • try without explicitly specifying errors in except should not be used (this construction should only be used if you really know what it is for) - you catch all exceptions and ignore them. do the handling of specific exceptions if they occur there.
  • write less "compressed" code - add new lines, they visually divide the code into logical blocks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question