0

I am trying to write the response received for ICMP query into excel sheet below is the sample code

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 has only headers and the response value is not written I tried few other things as well, but nothing worked so far Any idea whats wrong with this code?

Rupesh
  • 3
  • 1
  • 3

1 Answers1

2

Problems

You have 5 problems that I can see:

  1. You're not importing xlsxwriter. This is important for this to be an MCVE.
  2. The last 2 writes are indented one space (Python cares about this).
  3. You aren't closing the workbook so the file isn't being saved.
  4. Vars a, a1, and b should be more descriptive (this SO answer demonstrates why)
  5. You build a packet named a1 (renamed ping) and then build it again on the next line

Solution

The fixed code looks like this:

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()

Verification

Opening this in Libreoffice, we see that it has been saved: Libreoffice Spreadsheet

Ross Jacobs
  • 2,962
  • 1
  • 17
  • 27
  • Thanks Jacobs, code which i shared here was for demo purpose,I agree it should have written little better.However what worked for me was adding a delay of 5 sec , after that i can see the response in excel – Rupesh May 03 '20 at 20:12
  • Everyone starts somewhere:) You may want to add an edit section to your question for what worked for you, for the benefit of future readers. – Ross Jacobs May 03 '20 at 20:15