9

I have used scapy as a session in python, but I want to use it in a script. Why so? I want to be able to use sys.argv to specify an IP address to use as well as use other modules. How can this be accomplished?

James Parsons
  • 6,097
  • 12
  • 68
  • 108
  • 1
    if I get your question correctly, you dont want to use scapy Interface, but rather import scapy in other scripts right? you can do it by importing all.. 'from scapy.all import * ' should get you going – durga Apr 24 '14 at 12:57
  • that gives me an error that there is no `base_modules` module – James Parsons Apr 24 '14 at 13:02
  • can you put the complete error? – durga Apr 24 '14 at 13:03
  • 1
    looks like someone else too faced the error - http://stackoverflow.com/questions/17704520/scapy-problems-when-importing-modules?rq=1 – durga Apr 24 '14 at 13:06

1 Answers1

14

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())
Pierre
  • 6,047
  • 1
  • 30
  • 49
  • Hi @Pierre, I got this error when I tried to run the Scapy code embedded in a python script file.ubuntu@sdnhubvm:~[16:12]$ python scapy.py Traceback (most recent call last): File "scapy.py", line 8, in from scapy.all import * File "/home/ubuntu/scapy.py", line 8, in from scapy.all import * ImportError: No module named all Is this due to location of my python script? How to solve it. I want to just use scapy ping inside that script and Test if it is sending packet to the destination specified in dst. – Milson Sep 25 '14 at 23:35
  • 2
    You should probably not name your script `scapy.py`. – Pierre Sep 29 '14 at 13:35
  • when you do `scapy.sr(scapy.IP(dst=sys.argv[1])/scapy.ICMP())` what does the `\` mean/do?? – David Dias Oct 27 '14 at 17:39
  • Sorry @DavidDias but I don't understand your question. May be you have forgotten something between the `` chars? – Pierre Oct 27 '14 at 21:40
  • I'm sorry Pierre, you are right, there is a char missing, what does the `/` do then you have that expression? – David Dias Oct 28 '14 at 22:21
  • `/` is the encapsulation operator. That means ICMP encapsulated in IP. – Pierre Nov 03 '14 at 11:56