Skip to content

Instantly share code, notes, and snippets.

@namgk
Last active July 29, 2018 00:05
Show Gist options
  • Save namgk/6bf4d91dc4b6c3ed1f0e1019f1120ce6 to your computer and use it in GitHub Desktop.
Save namgk/6bf4d91dc4b6c3ed1f0e1019f1120ce6 to your computer and use it in GitHub Desktop.
Update /etc/hosts of two machines with pre configured host name. Helpful if there's no DNS resolve unit in the local network.
#!/bin/bash
if [ $# -eq 0 ]
then
echo "supply the peer IP address"
exit 1
fi
SUBNET_PREFIX=192
MY_HOST=me.test
PEER_HOST=me.test.peer
PEER_USER=
MY_ADDRESS=$(ifconfig | grep $SUBNET_PREFIX | awk '{ print $2 }')
echo "Setting $MY_HOST IP to" $MY_ADDRESS
echo "Setting $PEER_HOST IP to" $1
sed -i "s/.*$PEER_HOST/$1 $PEER_HOST/g; s/.*$MY_HOST$/$MY_ADDRESS $MY_HOST/g" /etc/hosts
echo "Testing"
ping -c 2 $MY_HOST
ping -c 2 $PEER_HOST
echo "Update peer hosts"
ssh -t $PEER_USER@$PEER_HOST "sudo sed -i '' 's/.*$PEER_HOST/$1 $PEER_HOST/g; s/.*$MY_HOST$/$MY_ADDRESS $MY_HOST/g' /etc/hosts"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment