Speed up Ollama host detection using ARP table + parallel checks

Instead of sequentially probing all 254 IPs in the subnet (worst case
~4 minutes), read the ARP table for devices already visible on the
network and check all of them for port 11434 simultaneously. Completes
in roughly one nc timeout (~1s) regardless of network size.
This commit is contained in:
cediackermann 2026-04-24 11:09:46 +02:00
parent fdab99ac79
commit 61a054f376

View file

@ -74,24 +74,28 @@ detect_ollama_host() {
return return
fi fi
local LOCAL_IP # Get IPs of devices currently visible on the network from the ARP table
LOCAL_IP=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || echo "") local ARP_IPS
if [ -z "$LOCAL_IP" ]; then 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
echo "" echo ""
return return
fi fi
local SUBNET print_warning "Ollama not found locally — checking $(echo "$ARP_IPS" | wc -l | tr -d ' ') network devices in parallel..."
SUBNET=$(echo "$LOCAL_IP" | cut -d. -f1-3)
print_warning "Ollama not found locally — scanning ${SUBNET}.0/24 for Ollama..."
# Check all ARP devices for port 11434 simultaneously
local TMPFILE
TMPFILE=$(mktemp)
while IFS= read -r ip; do
( nc -z -w 1 "$ip" 11434 2>/dev/null && echo "$ip" >> "$TMPFILE" ) &
done <<< "$ARP_IPS"
wait
local FOUND=() local FOUND=()
for i in $(seq 1 254); do while IFS= read -r ip; do
if nc -z -w 1 "${SUBNET}.${i}" 11434 2>/dev/null; then FOUND+=("$ip")
FOUND+=("${SUBNET}.${i}") done < "$TMPFILE"
fi rm -f "$TMPFILE"
done
if [ ${#FOUND[@]} -eq 1 ]; then if [ ${#FOUND[@]} -eq 1 ]; then
echo "${FOUND[0]}" echo "${FOUND[0]}"