This is a probe request packet sniffer using python/scapy. It sniffs Dot11ProbeReq packets and display the ssid, MAC of device and manufacturer name from the probe requests. Can also output the data to a log file if desired. Right now I feel the script is a bit slow and I can see it slow down from the first time I wrote it. It works but I know the code is nowhere as efficient as it could be. Can you point out my mistakes and let me know how I can write more efficient code?#!/usr/bin/env python# import all the needed librariesimport sysfrom netaddr import *import logginglogging.getLogger(scapy.runtime).setLevel(logging.ERROR)from scapy.all import *from subprocess import *import datetimeimport time# clear the consolecall([clear])                                           # set date-time parameters                                                          today = datetime.date.today()                 d=today.strftime(%d, %b %Y)tf=time.strftime( %H:%M)t=time.strftime( %H:%M:%S)# define variables                                                          clients = []                          uni = 0mach = []manu =[]# our packet handler                                                          def phandle(p):                           global uni        if p.haslayer(Dot11ProbeReq):                                 mac = str(p.addr2)        if p.haslayer(Dot11Elt):                                      if p.ID == 0:                 ssid = p.info                                             if ssid not in clients and ssid != :                    clients.append(ssid)                              maco = EUI(mac)            macf = maco.oui.registration().org               print len(clients),mac+ (+macf+) <--Probing--> +ssid            if args.log:                f.write (str(len(clients))+ +mac+ (+macf+) //+ <--Probing--> +ssid+\n)                if mac not in mach:                            mach.append(mac)                            uni+=1                                                  elif mac not in mach:            mach.append(mac)                        uni+=1                           # our main function             if __name__ == __main__:    import argparse    parser = argparse.ArgumentParser(description='PyRobe Help')    parser.add_argument('interface', action=store, help=specify interface (ex. mon0), default=False)    parser.add_argument(-l,--log, dest=log,action=store_true, help=print log file)    args = parser.parse_args()    if args.log:        f = open(ProbeLog+str(today)+str(tf)+.txt,w)            sniff(iface=args.interface,prn=phandle, store=0)                                print (\n)            print Unique MACs: ,uni        f.write (\nUnique MACs: +str(uni))        f.write (\nScan performed on: +str(d)+ at+str(t))          f.close()                                                         print Log successfully written. Exiting!    else:        sniff(iface=args.interface,prn=phandle, store=0)        print \nSuccessfully Exited! No log file written.I am basically looking for pointers on:Handling command line arguments betterCalculating and assigning required memory/avoid leaksShortening the code to make it faster to executeI am not looking for someone to edit my script and spoon feed me the solution, just point me in the right direction! My Python version: 2.7.3 (default, Mar 13 2014, 11:03:55) [GCC 4.7.2]My linux version: Distributor ID:Kali Description:Kali GNU/Linux 1.1.0 Release:1.1.0 Codename:moto Linux version 3.18.0-kali3-amd64 gcc version 4.7.2 Debian 4.7.2-5) ) #1 SMP Debian 3.18.6-1~kali2 (2015-03-02)
Probe request capturing with Scapy
python;linux;networking