0

我正在通过从 Google 获取引用来编写 python 脚本,我的任务是传递 IP 地址列表,检查 IP 是否响应,如果响应,则从端口 80 或 443 获取响应并匹配特定关键字响应,然后打印这些 IP 地址和响应的列表。截至目前,我只能通过运行该程序来获取所有端口都打开了,但我无法使用漂亮的汤向 IP 地址和端口发出请求。

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
4

0 回答 0