#!/bin/bash # scanPingLocalnet - Scan local net conf, ping whole local net, then run `host` # on each ip who answered to ping. # (C) 2025 Felix Hauri - felix@f-hauri.ch # This use forks to `ip` and `host` network tools scanToVars() { local __ip __net __iface __mask __int __tmp_iArry __tmp_string read -r __ip __net __iface < <(ip r g 1.1.1.1 | sed -ne 's@.*src \([0-9.]\+\) .*@ip a s |\ sed -ne \o47s_.*\1/\\([0-9]\\+\\) .* _\1 \\1 _p\o47@p' | sh); __mask=$(( ( ~0 ^ ~0<<32 ) ^ ( ~0 ^ ~0<<(32-$__net) ) )) __int=${__ip/./<<24|}; __int=${__int/./<<16|}; __int=${__int/./<<8|} mapfile -t range < <(seq $((__int & __mask + 1)) \ $(( ( ( ~0 ^ ~0<<32 ) ^ __mask|__int ) - 1 ))) local -ai __tmp_iArry=(${range[@]/%/>>24}) printf -v __tmp_string 'ping -nc1 -W1 %d.%%d.%%%%d.%%%%%%%%d &\n' ${__tmp_iArry[@]} local -ai __tmp_iArry=(${range[@]/%/>>16\&255}) printf -v __tmp_string "$__tmp_string" ${__tmp_iArry[@]} local -ai __tmp_iArry=(${range[@]/%/>>8\&255}) printf -v __tmp_string "$__tmp_string" ${__tmp_iArry[@]} local -ai __tmp_iArry=(${range[@]/%/\&255}) printf -v __tmp_string "$__tmp_string" ${__tmp_iArry[@]} sh -c "$__tmp_string" | sed -ne 's/^64.*from \([0-9.]\+\):.*$/host \1 \&/p' | sh |& sed -ne ' /^\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\.in-.*nter \(.*\)\.$/{ s//'${1:-host}'[\1|\2<<8|\3<<16|\4<<24]="\5"/p } /^Host \([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\).in.* found.*/{ s//'${1:-host}'[\1|\2<<8|\3<<16|\4<<24]="\4.\3.\2.\1"/p }' } source <(scanToVars presentList) declare -p presentList echo ${presentList[@]%.[^1-9]*}