Skip to content

Commit bbe9c42

Browse files
committed
fix subnet mask detection and add dependencies
1 parent 715cbb7 commit bbe9c42

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ jobs:
2323
- name: 📦 Install dependencies
2424
run: |
2525
python -m pip install --upgrade pip
26-
pip install nuitka scapy
26+
pip install nuitka
27+
pip install -r requirements.txt
2728
2829
- name: 📥 Pre-install Dependency Walker
2930
shell: powershell

network_scanner/scanner.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import platform
66
import logging
77
import asyncio
8+
import shutil
89
from scapy.all import ARP, Ether, conf, AsyncSniffer, sendp
910
from .utils.path_utils import resource_path
1011
from .utils.logger import CommonLogger
@@ -93,12 +94,11 @@ def get_device_name(ip, timeout=1):
9394
return "Unknown"
9495

9596
def get_subnet_mask(ip):
96-
if platform.system().lower() == "windows":
97+
system = platform.system().lower()
98+
99+
if system == "windows":
97100
try:
98101
output = subprocess.check_output("ipconfig", shell=True, text=True)
99-
os.makedirs("output", exist_ok=True)
100-
with open(os.path.join("output", "ipconfig_output.txt"), "w") as f:
101-
f.write(output)
102102
lines = output.splitlines()
103103
for i, line in enumerate(lines):
104104
if ip in line:
@@ -107,6 +107,48 @@ def get_subnet_mask(ip):
107107
return lines[j].split(":")[-1].strip()
108108
except Exception as e:
109109
logger.error(f"Error retrieving subnet mask: {e}")
110+
else:
111+
try:
112+
try:
113+
import netifaces # type: ignore
114+
115+
for iface in netifaces.interfaces():
116+
addrs = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])
117+
for addr in addrs:
118+
if addr.get("addr") == ip and addr.get("netmask"):
119+
return addr.get("netmask")
120+
except Exception as e:
121+
logger.debug(f"netifaces unavailable or failed: {e}")
122+
123+
if shutil.which("ip"):
124+
output = subprocess.check_output(
125+
["ip", "-o", "-f", "inet", "addr", "show"], text=True
126+
)
127+
for line in output.splitlines():
128+
parts = line.split()
129+
if len(parts) >= 4 and "/" in parts[3]:
130+
addr, prefix = parts[3].split("/")
131+
if addr == ip:
132+
return str(ipaddress.IPv4Network(f"0.0.0.0/{prefix}").netmask)
133+
134+
if shutil.which("ifconfig"):
135+
output = subprocess.check_output("ifconfig", shell=True, text=True)
136+
lines = output.splitlines()
137+
for i, line in enumerate(lines):
138+
if ip in line:
139+
for j in range(i, min(i + 5, len(lines))):
140+
if "netmask" in lines[j].lower():
141+
tokens = lines[j].split()
142+
if "netmask" in tokens:
143+
idx = tokens.index("netmask") + 1
144+
if idx < len(tokens):
145+
mask = tokens[idx]
146+
if mask.startswith("0x"):
147+
mask = str(ipaddress.IPv4Address(int(mask, 16)))
148+
return mask
149+
except Exception as e:
150+
logger.error(f"Error retrieving subnet mask: {e}")
151+
110152
return "255.255.255.0"
111153

112154
def get_ip_range():

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
scapy>=2.4.5
22
asyncio
3+
netifaces

0 commit comments

Comments
 (0)