#!/bin/bash

# pings all siblings from ~/.xs2siblings/siblings resp. /etc/xs2siblings except the own hostname

export SLF="/etc/xs2siblings/siblings"
test -e ~/.xs2siblings/siblings && SLF=~/.xs2siblings/siblings
if [ ! -f ${SLF} ] ; then
  echo "ERROR: No sibling list, i.e. no file ~/.xs2siblings/siblings or /etc/xs2siblings/siblings"
  exit 2
fi

SHN=$(hostname -s)
export SIBLINGS=$(cat ${SLF} |egrep -v '^\s*(#|;|$)' |egrep -vi "^${SHN}(\\.|$)" |sort |uniq |cut -f1 -d':')

if [ -z "$SIBLINGS" ] ; then
  echo "ERROR: No siblings specified in ${SLF}."
  exit 3
fi

if [ ! -x /usr/bin/ping ] ; then
  echo "ERROR: No /usr/bin/fping installed, please sudo apt install -y ping"
  exit 4
fi

for SIBLING in $SIBLINGS
do
  echo "Pinging $SIBLING ..."
  /usr/bin/ping -c3 $SIBLING
done

