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,50 +74,66 @@ detect_ollama_host() {
return return
fi fi
# Get IPs of devices currently visible on the network from the ARP table local LOCAL_IP
local ARP_IPS LOCAL_IP=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || echo "")
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 "$LOCAL_IP" ]; then
if [ -z "$ARP_IPS" ]; then
echo "" >&2 echo "" >&2
print_warning "No devices found in ARP table." >&2 print_warning "Could not determine local IP address." >&2
else else
local COUNT local SUBNET
COUNT=$(echo "$ARP_IPS" | wc -l | tr -d ' ') 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 echo "" >&2
print_warning "Scanning $COUNT network devices for Ollama (port 11434)..." >&2 print_warning "Ping-sweeping ${SUBNET}.0/24 to find live devices..." >&2
for i in $(seq 1 254); do
local TMPFILE ping -c1 -W1 "${SUBNET}.${i}" &>/dev/null &
TMPFILE=$(mktemp) done
while IFS= read -r ip; do
echo -e " ${BLUE}${NC} Checking $ip..." >&2
( nc -z -w 1 "$ip" 11434 2>/dev/null && echo "$ip" >> "$TMPFILE" ) &
done <<< "$ARP_IPS"
wait wait
local FOUND=() local ARP_IPS
while IFS= read -r ip; do ARP_IPS=$(arp -a 2>/dev/null | grep -v incomplete | grep -oE '\(([0-9]{1,3}\.){3}[0-9]{1,3}\)' | tr -d '()')
FOUND+=("$ip")
done < "$TMPFILE"
rm -f "$TMPFILE"
if [ ${#FOUND[@]} -eq 1 ]; then if [ -z "$ARP_IPS" ]; then
echo "${FOUND[0]}" print_warning "No live devices found on ${SUBNET}.0/24." >&2
return else
elif [ ${#FOUND[@]} -gt 1 ]; then local COUNT
echo "" >&2 COUNT=$(echo "$ARP_IPS" | wc -l | tr -d ' ')
print_warning "Multiple hosts with Ollama found:" >&2 print_warning "Checking $COUNT live devices for Ollama (port 11434)..." >&2
for i in "${!FOUND[@]}"; do
echo " $((i+1))) ${FOUND[$i]}" >&2 local TMPFILE
done TMPFILE=$(mktemp)
echo "" >&2
read -p "Select the Ollama host (1-${#FOUND[@]}): " pick <&2 while IFS= read -r ip; do
echo "${FOUND[$((pick-1))]}" echo -e " ${BLUE}${NC} Checking $ip..." >&2
return ( nc -z -w 1 "$ip" 11434 2>/dev/null && echo "$ip" >> "$TMPFILE" ) &
done <<< "$ARP_IPS"
wait
local FOUND=()
while IFS= read -r ip; do
FOUND+=("$ip")
done < "$TMPFILE"
rm -f "$TMPFILE"
if [ ${#FOUND[@]} -eq 1 ]; then
echo "${FOUND[0]}"
return
elif [ ${#FOUND[@]} -gt 1 ]; then
echo "" >&2
print_warning "Multiple hosts with Ollama found:" >&2
for i in "${!FOUND[@]}"; do
echo " $((i+1))) ${FOUND[$i]}" >&2
done
echo "" >&2
read -p "Select the Ollama host (1-${#FOUND[@]}): " pick <&2
echo "${FOUND[$((pick-1))]}"
return
fi
print_warning "Ollama not found on any live device." >&2
fi fi
echo "" >&2
print_warning "Ollama not found on any network device." >&2
fi fi
# Nothing found — ask the user directly # Nothing found — ask the user directly