Passing a list of I.P addresses and fetching the response to search a particular keyword in python

228 Views Asked by At

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
0

There are 0 best solutions below