簡體   English   中英

如何使用scapy獲取我自己的本地IP?

[英]How to get my own local IP using scapy?

我找不到僅使用scapy (而不是 Python 的 stdlib)來查找本地 IP 地址的方法。 我發現的唯一解決方法是發送一個虛擬包並使用它從源字段中檢索地址,但我覺得這不是一個好的解決方案。

如果您查看https://scapy.readthedocs.io/en/latest/routing.html#get-local-ip-ip-of-an-interface您可以使用以下方法獲取任何接口的本地 IP

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

快速提醒:您可能有不止一個 IP 地址。 您擁有的每個接口都有一個。 要獲取 scapy 當前工作接口的 IP 地址,您應該按照 Cukic0d 在他的評論中的建議執行get_if_addr(conf.iface)

這是一種適用於 Windows 和 Linux 的方法。 這將為您獲取所有接口的 IP 地址。

在 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'}

在蟒蛇殼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM