解决Python调用Scapy库找不到函数问题

Python使用Scapy库

本人使用的是pycharm,直接点击添加scapy库在这里插入图片描述
或者可以使用pip进行安装

sudo pip install scapy

引用形式

错误示范:

import    scapy                          
from   scapy  import    all             
from   scapy   import    *               

正确示范:

from scapy.all import *

ls():显示支持所有协议
lsc():显示支持所有命令(就是发送、接受数据包函数)
使用help(协议、命令),注意协议、命令不加单引号或者双引号,显示更详细帮助

简单构建一个数据包

send(IP(dst="192.168.1.1",src="192.168.1.123")/ICMP())

点击执行,发送成功
在这里插入图片描述

温馨提示:如果出现找不到IP类,可在头部添加

from scapy.layers.inet import IP,ICMP

自己编写的自动发送数据包源码分享一下

from scapy.all import *
from scapy.layers.inet import *


class Packets(object):

    ##生成完全随机的数据包
    def srpacket(self):
        while(1):
            packet=Ether(src=RandMAC(),dst=RandMAC())/IP(src=RandIP(),dst=RandIP())/ICMP()
            time.sleep(0.5)
            sendp(packet)
            print(packet.summary())

    ##发送随机MAC地址的数据包
    def srmac(self):
        while (1):
            packet = Ether(src=RandMAC(), dst=RandMAC())
            time.sleep(0.5)
            sendp(packet)
            print(packet.summary())

    ##发送随机IP地址的数据包
    def srip(self):
        while (1):
            packet = IP(src=RandMAC(), dst=RandMAC())/ICMP()
            time.sleep(0.5)
            sendp(packet)
            print(packet.summary())


a1 = Packets()
a1.srpacket()

おすすめ

転載: blog.csdn.net/qq_19623861/article/details/118513492