Scapy routing

Scapy needs to know many things related to the network configuration of your machine, to be able to route packets properly. For instance, the interface list, the IPv4 and IPv6 routes…

This means that Scapy has implemented bindings to get this information. Those bindings are OS specific. This will show you how to use it for a different usage.


Scapy will have OS-specific functions underlying some high level functions. This page ONLY presents the cross platform ones

List interfaces

Use get_if_list() to get the interface list

>>> get_if_list()
['lo', 'eth0']

You can also use the conf.ifaces object to get interfaces. In this example, the object is first displayed as as column. Then, the dev_from_index() is used to access the interface at index 2.

>>> conf.ifaces
SRC  INDEX  IFACE  IPv4       IPv6                      MAC
sys  2      eth0   fe80::10a:2bef:dc12:afae  Microsof:12:cb:ef
sys  1      lo  ::1                       00:00:00:00:00:00
>>> conf.ifaces.dev_from_index(2)
<NetworkInterface eth0 [UP+BROADCAST+RUNNING+SLAVE]>

IPv4 routes


If you want to change or edit the routes, have a look at the “Routing” section in Usage

The routes are stores in conf.route. You can use it to display the routes, or get specific routing

>>> conf.route

Network          Netmask          Gateway   Iface  Output IP  Metric  eth0   100   eth0   0   lo  1  eth0   100  eth0   100

Get the route for a specific IP: conf.route.route() will return (interface, outgoing_ip, gateway)

>>> conf.route.route("")
('lo', '', '')

IPv6 routes

Same than IPv4 but with conf.route6

Get router IP address

>>> gw = conf.route.route("")[2]
>>> gw

Get local IP / IP of an interface

Use conf.iface

>>> ip = get_if_addr(conf.iface)  # default interface
>>> ip = get_if_addr("eth0")
>>> ip

Get local MAC / MAC of an interface

>>> mac = get_if_hwaddr(conf.iface)  # default interface
>>> mac = get_if_hwaddr("eth0")
>>> mac

Get MAC by IP

>>> mac = getmacbyip("")
>>> mac