|
#!/bin/bash |
|
# this script should be run on the VMs' host to synchronize all VMs' and the host's /etc/hosts files |
|
|
|
# list all running VMs |
|
VMS=$(VBoxManage list runningvms) |
|
|
|
# create a base for VMs' /etc/hosts file |
|
echo "127.0.0.1 localhost.localdomain localhost" > /tmp/vmhosts |
|
echo "::1 localhost6.localdomain6 localhost6" >> /tmp/vmhosts |
|
|
|
# here we will collect fq-hostnames of all VMs that have corresponding properties set |
|
VMHNS="" |
|
|
|
for VM in $VMS |
|
do |
|
# VirtualBox lists VM names and UUIDs in its output; we are only interested in UUIDs |
|
if [[ "${VM:0:1}" == '{' ]] |
|
then |
|
# remove curly braces around VM UUID |
|
VM="${VM#\{}" |
|
VM="${VM%\}}" |
|
|
|
# get VM IP from VM's properties |
|
IP=$(VBoxManage guestproperty get $VM /VirtualBox/GuestInfo/Net/0/V4/IP | grep "Value: ") |
|
IP=${IP#Value\: } |
|
|
|
# get VM hostname from VM's properties |
|
HN=$(VBoxManage guestproperty get $VM /VirtualBox/GuestInfo/Net/0/V4/HN | grep "Value: ") |
|
HN=${HN#Value\: } |
|
|
|
# get VM fq-hostname from VM's properties |
|
FQHN=$(VBoxManage guestproperty get $VM /VirtualBox/GuestInfo/Net/0/V4/FQHN | grep "Value: ") |
|
FQHN=${FQHN#Value\: } |
|
|
|
# get VM fq-hostname from VM's properties |
|
ALTHN=$(VBoxManage guestproperty get $VM /VirtualBox/GuestInfo/Net/0/V4/ALTHN | grep "Value: ") |
|
ALTHN=${ALTHN#Value\: } |
|
|
|
if [[ -n "$VM" && -n "$IP" && -n "$HN" && -n "$FQHN" ]] |
|
then |
|
echo "Processing VM $VM - $IP - $FQHN - $HN - $ALTHN" |
|
# remove potentially existing old record for this VM in host's /etc/hosts file |
|
grep -v "\(^\|[^a-zA-Z0-9\-\.]\)${FQHN//./\\.}\([^a-zA-Z0-9\-\.]\|$\)" /etc/hosts | grep -v "\(^\|[^a-zA-Z0-9\-\.]\)${HN//./\\.}\([^a-zA-Z0-9\-\.]\|$\)" | grep -v "\(^\|[^a-zA-Z0-9\-\.]\)${IP//./\\.}\([^a-zA-Z0-9\-\.]\|$\)" > /tmp/hosts |
|
# add a line for this VM to the file |
|
echo -n "$IP $FQHN" >> /tmp/hosts |
|
for s in "$ALTHN" |
|
do |
|
echo -n " $s" >> /tmp/hosts |
|
done |
|
echo " $HN" >> /tmp/hosts |
|
# update /etc/hosts file on the host (assuming that current user's password is in ~/Private/pwd file) |
|
sudo -S -p "" /bin/bash -c "cp /tmp/hosts /etc/hosts" < ~/Private/pwd |
|
# add line for this VM to the VMs' /etc/hosts file |
|
echo -n "$IP $FQHN" >> /tmp/vmhosts |
|
for s in "$ALTHN" |
|
do |
|
echo -n " $s" >> /tmp/vmhosts |
|
done |
|
echo " $HN" >> /tmp/vmhosts |
|
VMHNS="$VMHNS $FQHN" |
|
fi |
|
fi |
|
done |
|
|
|
# get current host IP address and name (assuming there is only one non-127.0.* network interface) |
|
HIP=$(/sbin/ifconfig 2>&1 | grep 'inet addr:' | grep -v '127.0.' | cut -d: -f2 | awk '{ print $1}') |
|
HHN=$(hostname) |
|
HFQHN=$(hostname -f) |
|
# add a line for the host into /etc/hosts of all VMs |
|
echo "$HIP $HHN $HFQHN" >> /tmp/vmhosts |
|
|
|
# copy prepared VMs' /etc/hosts file to all VMs |
|
if [ -n "$VMHNS" ] |
|
then |
|
for VMHN in $VMHNS |
|
do |
|
scp -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no /tmp/vmhosts root@$VMHN:/etc/hosts |
|
done |
|
fi |