Skip to content

Commit

Permalink
Replace netifaces by scapy (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengfeihe authored Aug 14, 2024
1 parent 0122aba commit 0cc772e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ci/run_tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
netifaces==0.11.0
scapy==2.5.0
5 changes: 3 additions & 2 deletions ci/run_tests/run_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import subprocess
import argparse
import netifaces as ni
from scapy.all import get_if_addr

PCAP_FILE_PATH = os.path.join("Tests", "Pcap++Test", "PcapExamples", "example.pcap")

Expand Down Expand Up @@ -32,7 +32,8 @@ def main():
)
args = parser.parse_args()

ip_address = ni.ifaddresses(args.interface)[ni.AF_INET][0]["addr"]
ip_address = get_if_addr(args.interface)

print("IP address is: %s" % ip_address)

try:
Expand Down
25 changes: 22 additions & 3 deletions ci/run_tests/run_tests_windows.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import os
import argparse
import subprocess
import netifaces as ni
import scapy.arch.windows
from ipaddress import IPv4Address

TCPREPLAY_PATH = "tcpreplay-4.4.1-win"
PCAP_FILE_PATH = os.path.abspath(
os.path.join("Tests", "Pcap++Test", "PcapExamples", "example.pcap")
)


def validate_ipv4_address(address):
try:
IPv4Address(address)
return True
except ValueError:
return False


def get_ip_by_guid(guid):
interfaces = scapy.arch.windows.get_windows_if_list()
for iface in interfaces:
ips = iface.get("ips", [])
# Find the first valid IPv4 address inside iface["ips"]. If no address is found, return None
return next(filter(validate_ipv4_address, ips), None)
# Return None if no matching interface is found
return None


def find_interface():
completed_process = subprocess.run(
["tcpreplay.exe", "--listnics"],
Expand All @@ -26,8 +45,8 @@ def find_interface():
if len(columns) > 1 and columns[1].startswith("\\Device\\NPF_"):
interface = columns[1]
try:
ni_interface = interface.lstrip("\\Device\\NPF_")
ip_address = ni.ifaddresses(ni_interface)[ni.AF_INET][0]["addr"]
nic_guid = interface.lstrip("\\Device\\NPF_")
ip_address = get_ip_by_guid(nic_guid)
if ip_address.startswith("169.254"):
continue
completed_process = subprocess.run(
Expand Down

0 comments on commit 0cc772e

Please sign in to comment.