Ping-sweep subnet before ARP lookup to find devices not yet in cache

This commit is contained in:
cediackermann 2026-04-24 11:21:18 +02:00
parent 9fa3efacf6
commit 5b895a19b4

View file

@ -74,17 +74,33 @@ detect_ollama_host() {
return return
fi fi
# Get IPs of devices currently visible on the network from the ARP table local LOCAL_IP
LOCAL_IP=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || echo "")
if [ -z "$LOCAL_IP" ]; then
echo "" >&2
print_warning "Could not determine local IP address." >&2
else
local SUBNET
SUBNET=$(echo "$LOCAL_IP" | cut -d. -f1-3)
# Ping-sweep the subnet in parallel to populate the ARP cache,
# then read ARP for live hosts and check port 11434 on all of them.
echo "" >&2
print_warning "Ping-sweeping ${SUBNET}.0/24 to find live devices..." >&2
for i in $(seq 1 254); do
ping -c1 -W1 "${SUBNET}.${i}" &>/dev/null &
done
wait
local ARP_IPS local ARP_IPS
ARP_IPS=$(arp -a 2>/dev/null | grep -v incomplete | grep -oE '\(([0-9]{1,3}\.){3}[0-9]{1,3}\)' | tr -d '()') ARP_IPS=$(arp -a 2>/dev/null | grep -v incomplete | grep -oE '\(([0-9]{1,3}\.){3}[0-9]{1,3}\)' | tr -d '()')
if [ -z "$ARP_IPS" ]; then if [ -z "$ARP_IPS" ]; then
echo "" >&2 print_warning "No live devices found on ${SUBNET}.0/24." >&2
print_warning "No devices found in ARP table." >&2
else else
local COUNT local COUNT
COUNT=$(echo "$ARP_IPS" | wc -l | tr -d ' ') COUNT=$(echo "$ARP_IPS" | wc -l | tr -d ' ')
echo "" >&2 print_warning "Checking $COUNT live devices for Ollama (port 11434)..." >&2
print_warning "Scanning $COUNT network devices for Ollama (port 11434)..." >&2
local TMPFILE local TMPFILE
TMPFILE=$(mktemp) TMPFILE=$(mktemp)
@ -116,8 +132,8 @@ detect_ollama_host() {
return return
fi fi
echo "" >&2 print_warning "Ollama not found on any live device." >&2
print_warning "Ollama not found on any network device." >&2 fi
fi fi
# Nothing found — ask the user directly # Nothing found — ask the user directly