簡體   English   中英

使用 python 和 scapy 獲取連接到 WiFi 的所有 IP 地址

[英]Getting all IP addresses connected to WiFi using python and scapy

我如何獲得連接到 wifi 的所有 ips 的 IP 地址(我在上面)。 我嘗試通過使用 sniff() 並使用以下行獲取這些數據包的所有 src IP 來做到這一點:

ips = []
    captured = sniff(count=200)
        for i in range(len(captured)):
            try:
               rpp = captured[i].getlayer(IP).src
                if "192." in rpp and rpp != router_ip:
                    ips.append(rpp)
            ips = list(set(ips))

但這很少能讓我獲得所有 IP,那么我將如何使用 python 和(如果需要)scapy 來實現它?

如果我誤解了您的問題,請原諒我。您要做的是 map 局域網上的所有實時主機?

一種更簡單的方法是使用內置的ipaddresssocket庫。 對於 LAN 子網中的每個 IP,嘗試將套接字連接到各種端口 (TCP/UDP)。 如果建立了連接,則該 IP 中存在主機。

這是我能想到的一些代碼,可能會解決您的問題(我自己沒有測試過)

import socket
import ipaddress

live_hosts = []

# google popular tcp/udp ports and add more port numbers to your liking
common_tcp_ports = [23, 80, 443, 445]
common_udp_ports = [67, 68, 69]

def scan_subnet_for_hosts(subn = '192.168.1.0/24'):
    network = ipaddress.IPv4Network(subn)
    for ipaddr in list(network.hosts()):
        for port in common_tcp_ports:
             try:
                 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
                 s.connect((str(ipaddr), port))
                 live_hosts.append(ipaddr)
             except socket.error:
                 continue
        for port in common_udp_ports:
             try:
                 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
                 # UDP is connectionless, so you might have to try using s.send() as well
                 s.connect((str(ipaddr), port))
                 live_hosts.append(ipaddr)
             except socket.error:
                 continue

scan_subnet_for_hosts()
for host in live_hosts:
    print(f"Found live host: {str(host)}")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM