Skip to content

Instantly share code, notes, and snippets.

@pierew
Last active May 11, 2024 10:58
Show Gist options
  • Save pierew/5473dd90274058f67705 to your computer and use it in GitHub Desktop.
Save pierew/5473dd90274058f67705 to your computer and use it in GitHub Desktop.
A Little script that to the Avahi Discovery and put into /etc/hosts. Because my Avahi don't works well
#!/bin/bash
# Required Stuff
# * sed
# * grep
# * egrep
# * avahi-browse
# * cut
# * sort
# * awk
#
# Auto Discovery by paste "* * * * * /opt/avahi-discovery >/dev/null 2>&1" into Crontab
#
HOSTS_FILE=/etc/hosts
LHOST=( $(cat /etc/hostname | cut -d"." -f1) )
echo $LHOST
TMP_FILE=./avahi-discovery.tmp
touch $TMP_FILE
sed "/#Avahi Bindings/,/#Avahi Bindings END/d" $HOSTS_FILE -i
echo "Discovery started"
HOST_LIST=( $(avahi-browse -tpa | grep vboxnet | cut -d";" -f4 | cut -d\\ -f1 | egrep -v "$LHOST" | sort -u) )
discover() {
echo "Find Host DNS Name: "$1
HOST_IP=$(ping -c 1 $1 -n | grep "64 bytes from " | awk '{print $4}' | cut -d":" -f1)
echo "With IP: "$HOST_IP
echo $HOST_IP" "$1 >> $TMP_FILE
}
for HOST_NAME in ${HOST_LIST[@]}; do
TMP=$HOST_NAME".local"
discover $TMP
done
echo "Generating Hosts File..."
echo "#Avahi Bindings" >> $HOSTS_FILE
sort $TMP_FILE --output=$TMP_FILE
cat $TMP_FILE >> $HOSTS_FILE
echo "#Avahi Bindings END" >> $HOSTS_FILE
cat $HOSTS_FILE
rm -rf $TMP_FILE
unset TMP
unset TMP_FILE
unset HOSTS_FILE
unset HOST_NAME
unset HOST_IP
unset LHOST
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment