You just need to import it, as any other Python module.
from scapy.layers.inet
import IP, ICMP
from scapy.sendrecv
import sr
import sys
sr(IP(dst = sys.argv[1]) / ICMP())
Or if you want to import everything at once:
import scapy.all as scapy
import sys
scapy.sr(scapy.IP(dst = sys.argv[1]) / scapy.ICMP())[...]
Or if you want to code exactly as in the Scapy console:
from scapy.all
import *
import sys
sr(IP(dst = sys.argv[1]) / ICMP())
This first example takes an IP or a name as first parameter, send an ICMP echo request packet and display the completely dissected return packet:,Here is another tool that will constantly monitor all interfaces on a machine and print all ARP request it sees, even on 802.11 frames from a Wi-Fi card in monitor mode. Note the store=0 parameter to sniff() to avoid storing all packets in memory for nothing:,Scapy configures a logger automatically using Python’s logging module. This logger is custom to support things like colors and frequency filters. By default, it is set to WARNING (when not in interactive mode), but you can change that using for instance:,Once you’ve done that, you can launch Scapy and import your file, but this is still not very convenient. Another way to do that is to make your file executable and have it call the Scapy function named interact():
#! /usr/bin/env python import sys from scapy.all import sr1, IP, ICMP p = sr1(IP(dst = sys.argv[1]) / ICMP()) if p: p.show()
import logging
logging.getLogger("scapy").setLevel(logging.CRITICAL)
#! /usr/bin/env python
# arping2tex : arpings a network and outputs a LaTeX table as a result
import sys
if len(sys.argv) != 2:
print("Usage: arping2tex <net>\n eg: arping2tex 192.168.1.0/24")
sys.exit(1)
from scapy.all import srp, Ether, ARP, conf
conf.verb = 0
ans, unans = srp(Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=sys.argv[1]),
timeout=2)
print(r"\begin{tabular}{|l|l|}")
print(r"\hline")
print(r"MAC & IP\\")
print(r"\hline")
for snd,rcv in ans:
print(rcv.sprintf(r"%Ether.src% & %ARP.psrc%\\"))
print(r"\hline")
print(r"\end{tabular}")
#! /usr/bin/env python
from scapy.all
import *
def arp_monitor_callback(pkt):
if ARP in pkt and pkt[ARP].op in (1, 2): #who - has or is - at
return pkt.sprintf("%ARP.hwsrc% %ARP.psrc%")
sniff(prn = arp_monitor_callback, filter = "arp", store = 0)
#! /usr/bin/env python # Set log level to benefit from Scapy warnings import logging logger = logging.getLogger("scapy") logger.setLevel(logging.INFO) from scapy.all import * class Test(Packet): name = "Test packet" fields_desc = [ShortField("test1", 1), ShortField("test2", 2) ] def make_test(x, y): return Ether() / IP() / Test(test1 = x, test2 = y) if __name__ == "__main__": interact(mydict = globals(), mybanner = "Test add-on v3.14")
# ./test_interact.py Welcome to Scapy (0.9.17.109beta) Test add-on v3.14 >>> make_test(42,666) <Ether type=0x800 |<IP |<Test test1=42 test2=666 |>>>
Last Updated : 01 Mar, 2020
Installation of scapy module:
As scapy module is not included in Python3 library by default, we have to add it into our Python library using pip. Execute this command in your Linux terminal to get the scapy module for Python3.
pip3 install scapy - python3