0

I was writing a python script by taking references from Google and my task is to pass a list of I.P address ,check if the I.P is responsive or not , if responsive, fetch the response from port 80 or 443 and match a particular keyword in the response and then print the list of those I.P addresses and response. As of now I'm only able to get which all ports are open by running this program but I'm unable to make the request using beautiful soup to the I.P addresses and ports.

import socket
import re
from bs4 import BeautifulSoup
import requests


f = open('ip_list.txt' , 'r') ## Read File
o = f.read()

ip1 = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", o )

hosts = ip1
ports = [80,443] ## Include the list of ports which needs to be checked

for host in hosts:
    for port in ports:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.settimeout(10)
            result = s.connect_ex((host,port))
            if result == 0:
                print("  [*] Port " + str(port) + " open! " + host)
                req = requests.get(result)
                if req.status_code == 200 :
                    soup = BeautifulSoup(req.text)
                    tag = soup.find(text="particular keyword")
                    print(" Keyword Found is " + str(tag))

            else: print("  [*] Port " + str(port) + " close! " + host)

        except:
            pass
cradlr
  • 11
  • 3
  • What errors are you getting? Its hard to tell whats going wrong with this code. – RoadRunner Apr 13 '20 at 07:00
  • can you clarify what you mean by this ```I'm unable to make the request using beautiful soup to the I.P addresses and ports```? – AzyCrw4282 Apr 13 '20 at 07:03
  • @RoadRunner -No errors, actually , with this code I'm only getting the output for the I.P's which are active on port 443 and 80, however the below code is not executing. from req = requests.get(result) till print(" Keyword Found is " + str(tag)) – cradlr Apr 13 '20 at 07:37
  • @AzyCrw4282 - I'm making use of beautifulsoup to scrape the content of the response but in my case req = requests.get(result) is not working as in I'm not sure how to pass I.P's and ports to requests and then fetch a response. – cradlr Apr 13 '20 at 07:41
  • These are public ips right? Why not just use the domain name instead? – AzyCrw4282 Apr 13 '20 at 08:11
  • @AzyCrw4282 - Yes, all are public I.P's and I do agree using a host-name will fetch me results but I wanted to check using I.P addresses as the list is quite big and resolving the DNS will take some time with respect to this program. – cradlr Apr 13 '20 at 08:29
  • I dont think you can solve your problem without the host name. Note that, each HTTP request includes a Host: header that tells the HTTP server what domain it wants to reach. This exists so that multiple domains can live on a single IP. Therefore using a single ip to make an http doesnt suffice for this problem. See [here](https://www.hcidata.info/host2ip.cgi) for some conversion. – AzyCrw4282 Apr 14 '20 at 01:14

0 Answers0