-1

I am trying to write 'data' from Scapy SNIFF to a CSV directly when python script for SNIFF is running, but I got the following error while running the script:

py_compile.PyCompileError: Sorry: IndentationError: unexpected indent (prog.py, line 33)

What is going wrong here?

Here is my code:

from scapy.all import *
import datetime
import csv
import sys

PROBE_REQUEST_TYPE=0
PROBE_REQUEST_SUBTYPE=4

STAMP = datetime.datetime.now().isoformat()

WHITELIST = ['00:00:00:00:00:00',] # Replace this with your phone's MAC address


def PacketHandler(pkt):
if pkt.haslayer(Dot11):
    if pkt.type==PROBE_REQUEST_TYPE and pkt.subtype == PROBE_REQUEST_SUBTYPE and ( pkt.addr2.lower() not in WHITELIST or pkt.addr2.upper() not in WHITELIST):
        PrintPacket(pkt)

def PrintPacket(pkt):
print "Probe Request Captured:"
try:
    extra = pkt.notdecoded
except:
    extra = None
if extra!=None:
    signal_strength = -(256-ord(extra[-4:-3]))
else:
    signal_strength = -100
    print "No signal strength found"    
print "Target: %s Source: %s SSID: %s RSSi: %d"%(pkt.addr3,pkt.addr2,pkt.getlayer(Dot11ProbeReq).info,signal_strength)
f = open('/mtn/sda1/logger.csv', 'w')
filename = 'logger.csv'.format(STAMP)
    open(filename, 'a') as f:
           f.write('Target: %s Source: %s SSID: %s RSSi: %d"%(pkt.addr3,pkt.addr2,pkt.getlayer(Dot11ProbeReq).info,signal_strength)'.format(STAMP))
f.close()

def main():
from datetime import datetime
print "[%s] Starting scan"%datetime.now()
print "Scanning ..."
print "\n"
sniff(iface=sys.argv[1],prn=PacketHandler,store=0)

if __name__=="__main__":
main()
Chrisvr
  • 9
  • 4

1 Answers1

1

you're missing the with keyword:

filename = 'logger.csv'.format(STAMP)
     open(filename, 'a') as f:
           f.write('Target: %s Source: %s SSID: %s RSSi: %d"%(pkt.addr3,pkt.addr2,pkt.getlayer(Dot11ProbeReq).info,signal_strength)'.format(STAMP))
f.close() # unnecessary

also, if you're using the with keyword, you don't need to close the file manually.

yurib
  • 8,043
  • 3
  • 30
  • 55
  • Thanks I still have a problem.. I put the 'with' in it and deleted 'f.close()' but now the error that I get is: File "sas.py", line 33 f.write('Target: %s Source: %s SSID: %s RSSI: %d"%(pkt.addr3,pkt.addr2,pkt.getlayer(Dot11ProbeReq).info,signal_strength)'.format(STAMP)) ^ IndentationError: expected an indented block ANY IDEAS what is wrong with my code? – Chrisvr Sep 10 '15 at 12:57
  • @Chrisvr http://www.diveintopython.net/getting_to_know_python/indenting_code.html – yurib Sep 10 '15 at 13:01