#!/bin/bash # manyPing.sh - How to collect many ping's outputs into bash variables # (C) 2025 Felix Hauri - felix@f-hauri.ch # Licensed under terms of GPL v3. www.gnu.org # This script is intented to be used under GNU/Linux, as they use kernel # pseudo filesystem /proc (for retrieving mac address, in `getArps` function). # Collecting networks IPv4 status and infos from parallelized ping's answer, # into 5 differents bash arrays: # - $byIp Indexed array: ([167904045]='hosta' [167904046]='hostb'... # - $allIPS Indexed array: ([167904045]='10.2.3.45' [167904046]='10.2.3.46' # - $state Indexed array: ([167904045]='1' [167904046]='0'... # - $allUp Indexed array: ([167904045]='hosta' ...) # - $allDown Indexed array: ([167904046]='hostb' ...) # Note: In order to avoid bash loops, all array operation are done by "mapping" # ( While keeping in mind `declare -a "array=($str)"` is like an `eval`!! ) # In a second operation, another variable from: mapfile [ip or hostname] [ip or hostname]... # -d Show sed generated bash, temp string for $listArps and all 6 arrays. # Try maybe this:$ manyPing $(awk '/^[1.9]/{if($3)print $3}' &2 "Arguments missing!" sed -ne "/^# Usage/,+2{s|manyPing|$0|;s/^# //;p}" "$0" >&2 exit 1 fi started=$EPOCHREALTIME declare -a byIp=() state=() allIPs=() listArps=() allUp=() allDown=() source <( # run parallels ping and execute bash directives generated by sed for target; do ping -n4c1 -W1 $target & done | out2var ) getArps printOut $debug && dumpArray listArps byIp state all{IPs,Down,Up} >&2 ended=$EPOCHREALTIME elap=00000$(( ${ended/.} - ${started/.} )) printf '%d hosts processed in %.3f secs.\n' ${#byIp[@]} ${elap::-6}.${elap: -6}