扫描IP段内的使用的IP

扫描IP段内的使用的IP

方法一:命令行

命令行进入

bash 复制代码
for /L %i IN (1,1,254) DO ping -w 1 -n 1 192.168.3.%i
bash 复制代码
arp -a

方法二:python

python 复制代码
from scapy.all import ARP, Ether, srp
import keyboard


def scan_network(ip_range):
    # 创建一个ARP请求包
    arp = ARP(pdst=ip_range)
    ether = Ether(dst="ff:ff:ff:ff:ff:ff")
    packet = ether / arp

    # 发送包并接收响应
    result = srp(packet, timeout=2, verbose=0)[0]

    # 解析响应
    devices = []
    for sent, received in result:
        devices.append({'ip': received.psrc, 'mac': received.hwsrc})

    return devices


if __name__ == "__main__":
    # 指定要扫描的网段
    target_ip_range = str(input("请输入IP(格式:192.168.20.0/23):"))
    # target_ip_range = "192.168.20.0/23"
    active_devices = scan_network(target_ip_range)
    print("Active devices in the network:")
    for device in active_devices:
        print(f"IP: {device['ip']}, MAC: {device['mac']}")
    print("按空格键退出")
    # 检查是否有按键被按下
    while True:
        if keyboard.is_pressed('space'):
            print("Exiting...")
            break

可以用pyinstaller打包成exe来处理,这个比方法一会快一点。

还有优化空间,因为目前是单线程来的

相关推荐
爱编程的Zion7 小时前
【踩坑记录】同一个 WiFi 下,A 电脑能访问 192.168.0.115,B 电脑死活不行 ——Windows 路由表缺失引发的故障排查
windows·电脑
Qwier9 小时前
Win Srv 2019 安装补丁后重启
运维·服务器·windows
lusklusklusk10 小时前
Python 四大数据结构详解:元组、列表、集合、字典的特点与增删改查
数据结构·windows·python
今年下半年10 小时前
【运维】windows虚拟机安装arm(aarch64)服务器
运维·windows·arm·虚拟机·aarch64
浪潮IT馆11 小时前
Windows 10 安装 PostgreSQL 9.6.24 完整教程
数据库·windows·postgresql
sukalot12 小时前
Windows 驱动实例分析系列:libwdi 驱动分析 - libwdi 篇(一)
windows
奇树谦12 小时前
Windows 下使用 PowerShell 和 CMD 排查端口占用
windows
万年咸鱼1 天前
Qt Windows 使用管理员权限运行 Cmd
windows
2601_962364971 天前
从录入到解锁,完整梳理 Windows Hello 识别链路
windows·安全·电脑
hacker_LeeFei1 天前
Windows 下配置 Redis 8.10.2 开机自启:一次踩坑全记录
数据库·windows·redis