7

I couldn't find a way to find my local IP address using ONLY scapy (and not the Python's stdlib). The only workaround I found is sending a dummy package and using it to retrieve the address from the source field, but I don't feel like it is a good solution.

dec0de_d00dle
  • 425
  • 1
  • 4
  • 13
  • 2
    This question is about Scapy, not Python standard library, and there is, in Scapy, a specific way to do what the OP wants. – Pierre Feb 28 '18 at 14:38
  • 1
    (and by the way that's an interesting question) – Pierre Feb 28 '18 at 14:39
  • 1
    from my understanding from [here](https://scapy.readthedocs.io/en/latest/usage.html), the way to do it with scapy seems to be: `IP().src`. Altough this creates a dummy packet, the dummy packet is not sent. – charelf Nov 02 '18 at 10:27

2 Answers2

4

If you have a look at https://scapy.readthedocs.io/en/latest/routing.html#get-local-ip-ip-of-an-interface you can get the local IP of any of your interfaces using

>>> ip = get_if_addr(conf.iface)  # default interface
>>> ip = get_if_addr("eth0")
>>> ip
'10.0.0.5'
Cukic0d
  • 5,111
  • 2
  • 19
  • 48
1

Quick reminder: you might have more then one IP address. There's one for each interface you have. To get the IP address for scapy's current working interface you should do get_if_addr(conf.iface) as suggested by Cukic0d in his comment.

Here's a way that will work in both windows and linux. This will get you the ip address for all your interfaces.

Working in scapy shell

>>> s = set() # there will be redundencies so we'll use set to remove them
>>> for line in read_routes():
...:     s.add(line[4])
...:
>>> s
{'10.0.0.4',
 '169.254.106.110',
 '169.254.17.51',
 '169.254.177.137',
 '192.168.56.1',
 '192.168.99.1'}

In python shell

>>> import scapy.all as S
>>> s = set() # there will be redundencies so we'll use set to remove them
>>> for line in S.read_routes():
...:     s.add(line[4])
...:
>>> s
{'10.0.0.4',
 '169.254.106.110',
 '169.254.17.51',
 '169.254.177.137',
 '192.168.56.1',
 '192.168.99.1'}
barshopen
  • 1,190
  • 2
  • 15
  • 28