-
-
Save chaffeqa/8922736 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# hosts-to-vm.sh | |
# Drew Reece - September 2011 | |
# | |
# Made for use alongside the excellent ievms - https://github.com/xdissent/ievms | |
# | |
# Will export the local hosts (from /etc/hosts) | |
# to a batch file & add that batch file to a Windows VM | |
# The batch script will be executed to import the hosts onto the VM | |
# | |
# REQUIREMENTS : | |
# VirtualBox & VirtualBox Guest Additions installed (v4.0 or greater) on the guest | |
# Local hosts defined in /etc/hosts using 127.0.0.1 address. | |
# | |
# USAGE : | |
# Have the VM running before using the script. | |
# Change the script if you want to avoid overwriting the Windows hosts file. | |
# The batch file seems convoluted, until you only want to append the new hosts. | |
# Call the script a follows on the main host. | |
# hosts-to-vm.sh VM-NAME "VM - Name" | |
# The console should flash up & dissapear on the VM, the batch file will remain in C:\ | |
# | |
# The new hosts use the local machines IP address, since 127.0.0.1 will resolve to the VM, not the host OS. | |
# | |
# OVERVIEW : | |
# 1 Parse local (127.0.0.1 ONLY) hosts to a list | |
# 2 Create a .bat file to copy to the Windows VM disk | |
# 3 Loop over hosts, adding commands to batchfile. | |
# 4 Copy batchfile to C:\ of Administrator in the VM | |
# 5 Execute the batch | |
# The generated batch file | |
BATCHFILE="/tmp/hosts.bat" | |
HOSTS="/etc/hosts" | |
SCRIPT=`basename "$0"` | |
IP=`ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2` | |
# Reqires VM to be running, pass it as arg1 | |
if [[ -z $1 ]]; then | |
echo "Specify at least one VM to use, e.g. | |
$SCRIPT VM-NAME | |
$SCRIPT vm1 [vm2 ... vm3]" | |
exit 0 #statements | |
fi | |
# Get local hostnames on the host machine | |
function get_hosts() { | |
HOSTLIST=`awk '/localhost/ || /broadcasthost/ || /^#/ {next}; /127.0.0.1/ {print $2}' $HOSTS` | |
# Setup our batch file | |
touch $BATCHFILE | |
# Will OVERWRITE hosts file on VM unless you remove the echo. >NUL 2>%hostspath% | |
cat > $BATCHFILE <<"BATCHHEADER" | |
@echo off | |
set hostspath=%windir%\System32\drivers\etc\hosts | |
echo. >NUL 2>%hostspath% | |
BATCHHEADER | |
# for each host add a line to create it in our batchfile... | |
#echo local-IP-addr host.name >> %hostspath% | |
for host in $HOSTLIST | |
do | |
echo "echo $IP $host >> %hostspath%" >> $BATCHFILE | |
done | |
# Add the script closure to the batch file | |
cat >> $BATCHFILE << 'CLOSURE' | |
exit | |
CLOSURE | |
} | |
get_hosts | |
# Quote to allow for spaces in arguments | |
for VM in "$@" | |
do | |
STATE=`VBoxManage showvminfo "$VM" | awk '/State/ {print $2}'` | |
if [[ $STATE != 'running' ]]; then | |
echo "The Virtual machine $VM is not running ... ignoring it" | |
else | |
# Import the batch to the VM. (using windows back slashes fails) | |
VBoxManage guestcontrol copyto $VM $BATCHFILE C:/hosts.bat --username Administrator --password Password1 --verbose | |
# and Run it... | |
VBoxManage guestcontrol exec $VM C:/hosts.bat --username Administrator --password Password1 --verbose | |
fi | |
done | |
# Cleanup local copy | |
rm $BATCHFILE | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This would fit nicely to iectrl