简体   繁体   中英

How to specify a scapy startup file?

I use scapy with the -c command line option to load a startup file:

# liquidsoap debug
streamerIP = "192.168.0.53"
dump= []

def filterStreamer(pkt):
    if pkt.src == streamerIP or pkt.dst == streamerIP:
        dump.append(pkt)

sniff(prn=filterStreamer)

ls(dump)

it gives:

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/scapy/main.py", line 30, in _read_config_file
    execfile(cf)
  File "icecast-debug.py", line 9, in <module>
    sniff(prn=filterStreamer)
  File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 586, in sniff
    r = prn(p)
  File "icecast-debug.py", line 6, in filterStreamer
    if (pkt.src == streamerIP or pkt.dst == streamerIP):
NameError: global name 'streamerIP' is not defined
Welcome to Scapy (2.2.0)

and in the console I see nor the streamerIP neither dump, but funniest of all filterStreamer as a function are not defined. However if I do not pass filterStreamer to sniff it begins sniffing. So it's like interpreting the code line by line, and clear the scope after all line interpretition.

You have to use the global keyword. Also, use a PacketList() rather than a list. And ls() will not work against a list, but if you use a PacketList() , you have the .summary() method.

streamerIP = "192.168.0.53"
dump = PacketList()

def filterStreamer(pkt):
    global streamerIP, dump
    if pkt.src == streamerIP or pkt.dst == streamerIP:
        dump.append(pkt)

dump.summary()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM