Skip to content

Instantly share code, notes, and snippets.

@kvaps
Created January 5, 2017 23:21
Show Gist options
  • Save kvaps/f0f5b82069776cc0d270a450d8c96325 to your computer and use it in GitHub Desktop.
Save kvaps/f0f5b82069776cc0d270a450d8c96325 to your computer and use it in GitHub Desktop.
#!/bin/bash
REPEAT=120 #2 minutes
# Wait for file
until [ -f "$1" ]; do
sleep 1
# Timeout check
if [ "$REPEAT" -gt 0 ]; then
((REPEAT--))
else
exit 1
fi
done
# Remove exist hostname
sed -i '/ '$(hostname)'$/d' "$1" 2> /dev/null
# Write new hostname
echo "$(ip addr list | grep -oP '(?<=inet )([0-9]+.){3}[0-9]+' | tail -n1) $(hostname)" >> "$1"
# replace default hosts-file
rm -f /etc/hosts
ln -s "$1" /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment