I setup 4 virtualbox vms which have dynamic ip (my env uses dhcp ).
Now I need to ssh to each vm so when ip is changed, I have to check vm's ip and update /etc/hosts so that when next time ssh user@vm_host would work.
So I wrote a simple script and want to update /etc/hosts once vms are started up. But it seems the content of /etc/hosts won't be changed after executing the script, and the console shows '127.0.0.1: not found' What may go wrong? And hot to fix it?
Below is the script content.
Code:
for i in 1 2 3 4
do
IP=`VBoxManage guestproperty get box$i "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{print $2}'`
`sudo sed 's/^(.*) box$i\.domain\.com box$i/^$IP box$i\.domain\.com box$i/g' /etc/hosts`
done
/etc/hosts looks as below:
Code:
127.0.0.1 localhost
192.168.1.111 my_host.domain.com my_host
192.168.1.112 box1.domain.com box1
192.168.1.114 box2.domain.com box2
192.168.1.159 box3.domain.com box3
192.168.1.199 box4.domain.com box4
Thanks for advice.