Skip to content

Commit

Permalink
use scapy to as the new option
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengfeihe committed Jul 14, 2024
1 parent 56e56e5 commit e6474c0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
3 changes: 1 addition & 2 deletions ci/run_tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
psutil==6.0.0
wmi==1.5.1
scapy==2.5.0
15 changes: 2 additions & 13 deletions ci/run_tests/run_tests.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import os
import subprocess
import argparse
import psutil
import socket
from scapy.all import get_if_addr

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


def get_ip_address(interface):
addresses = psutil.net_if_addrs().get(interface)
if not addresses:
return None
for address in addresses:
if address.family == socket.AF_INET:
return address.address
return None


def main():
parser = argparse.ArgumentParser()
parser.add_argument("--interface", type=str, required=True, help="interface to use")
Expand All @@ -43,7 +32,7 @@ def main():
)
args = parser.parse_args()

ip_address = get_ip_address(args.interface)
ip_address = get_if_addr(args.interface)

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

Expand Down
32 changes: 12 additions & 20 deletions ci/run_tests/run_tests_windows.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import os
import argparse
import subprocess
import psutil
import socket
import wmi
import scapy.arch.windows

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


def get_ip_address(interface):
c = wmi.WMI()
nic_configs = c.Win32_NetworkAdapter()
for nic in nic_configs:
if nic.GUID and nic.GUID.lower() == interface.lower():
addresses = psutil.net_if_addrs().get(interface)
if not addresses:
def get_ip_by_guid(guid):
interfaces = scapy.arch.windows.get_windows_if_list()
for iface in interfaces:
if iface["guid"] == guid:
if len(iface["ips"]) > 0:
return iface["ips"][0]
else:
return None
for address in addresses:
if address.family == socket.AF_INET:
return address.address
return None


Expand All @@ -42,8 +37,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 = get_ip_address(ni_interface)
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 Expand Up @@ -80,11 +75,8 @@ def main():

tcpreplay_interface, ip_address = find_interface()
if not tcpreplay_interface or not ip_address:
print(
"Cannot find an interface to run tests on! Info from psutil.net_if_addrs() %s"
% psutil.net_if_addrs()
)
exit(1)
print("Cannot find an interface to run tests on!")
exit(1)
print(f"Interface is {tcpreplay_interface} and IP address is {ip_address}")

try:
Expand Down

0 comments on commit e6474c0

Please sign in to comment.