Skip to content

Instantly share code, notes, and snippets.

@kvaps
Last active January 11, 2017 22:11
Show Gist options
  • Select an option

  • Save kvaps/3dabf0aaec05af647568523002285dda to your computer and use it in GitHub Desktop.

Select an option

Save kvaps/3dabf0aaec05af647568523002285dda to your computer and use it in GitHub Desktop.
#!/bin/bash
# Wait for file
until [ -f "$1" ]; do sleep 5 ; 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