You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Scapy实现网络端口流量转发失败问题排查

Scapy网卡流量转发失败排查

实验环境

  • 树莓派通过以太网连接Linux系统笔记本
  • Linux笔记本通过无线适配器连接家庭路由器,路由器默认IP为192.168.0.1
  • 树莓派IP通过命令sudo ifconfig eth0 192.168.0.117手动配置

问题需求与现象

希望实现树莓派以太网流量与路由器无线流量的双向转发,让树莓派终端能ping通路由器。但编写的Scapy代码执行无报错,树莓派执行ping 192.168.0.1时提示Destination Host Unreachable,未达到预期效果。

尝试的代码实现

多进程转发代码

from multiprocessing import Process
from scapy.all import *

def thread1():
    while 1:
        pkt = sniff(iface="wlo1", count=1)[0]
        sendp(pkt, iface="enp3s0")
        #send(pkt, iface="enp3s0")

def thread2():
    while 1:
        pkt = sniff(iface="enp3s0", count=1)[0]
        sendp(pkt, iface="wlo1")
        #send(pkt, iface="wlo1")

p1 = Process(target=thread1)
p2 = Process(target=thread2)

p1.start()
p2.start()

p1.join()
p2.join()

后续排查与更新

  1. 用Wireshark在两台设备上抓包,确认流量在网卡间桥接正常,能观测到ICMP echo request从以太网传到无线适配器,但始终收不到路由器的echo reply
  2. 尝试使用Scapy的bridge_and_sniff函数实现转发,代码如下,问题依旧:
from scapy.all import *

def pkt_callback_xfrm12(pkt):
    print("from wlo1")
    print(pkt.summary())
    print("-------------------------------\n\n")
    return True

def pkt_callback_xfrm21(pkt):
    print("from enp3s0")
    print(pkt.summary())
    print("-------------------------------\n\n")
    return True

bridge_and_sniff(if1="wlo1", if2="enp3s0", xfrm12=pkt_callback_xfrm12, xfrm21=pkt_callback_xfrm21)

内容的提问来源于stack exchange,提问作者Aztec N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:55:17