无法将 ICMP 响应写入 excel

Unable to write the ICMP response into excel

我正在尝试将收到的 ICMP 查询响应写入 excel sheet 下面是示例代码

from scapy.layers.inet import *
a1= IP(dst="192.168.1.1")/ICMP()
a = sr1(IP(dst="192.168.1.1")/ICMP(), iface="Wireless Network Connection", timeout=10)
b = a.summary()

# Create file workbook and worksheet
outworkbook = xlsxwriter.Workbook('output.xlsx')
outsheet = outworkbook.add_worksheet()

# writing headers
outsheet.write("A1", "Requests")
outsheet.write("B1", "Response")

 # writing data
 outsheet.write("A2", a1)
 outsheet.write("B2", b)

created excel只有headers,没有写入response value 我也尝试了一些其他的东西,但到目前为止没有任何效果 知道这段代码有什么问题吗?

问题

我看到你有 5 个问题:

  1. 您没有导入 xlsxwriter。这对于 MCVE.
  2. 很重要
  3. 最后 2 次写入缩进一个 space(Python 关心这个)。
  4. 您没有关闭工作簿,因此没有保存文件。
  5. Vars aa1b 应该更具描述性(this SO answer 说明了原因)
  6. 你构建了一个名为 a1 的数据包(重命名为 ping),然后在下一行再次构建它

解决方案

固定代码如下所示:

import xlsxwriter
from scapy.layers.inet import *

# My firewall is at 192.168.1.254 so use that address; en0 is my interface
ping_packet = IP(dst="192.168.1.254")/ICMP()
answer = sr1(ping_packet, iface="en0", timeout=10)
ping_summary = answer.summary()

# Create file workbook and worksheet
outworkbook = xlsxwriter.Workbook('output.xlsx')
outsheet = outworkbook.add_worksheet()

# writing headers
outsheet.write("A1", "Requests")
outsheet.write("B1", "Response")

# writing data
outsheet.write("A2", a1)
outsheet.write("B2", b)

# close file
outworkbook.close()

验证

在 Libreoffice 中打开它,我们看到它已被保存: