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']

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