Skip to content

Instantly share code, notes, and snippets.

@clavery
Last active August 29, 2015 14:03
Show Gist options
  • Save clavery/501ec902ee99b3870437 to your computer and use it in GitHub Desktop.
Save clavery/501ec902ee99b3870437 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
## Port map and set static address for Natted VM Fusion box
## Box must be running and this script needs to be run as sudo
## Restart VMware after
##
## sudo sh fusionportmap.sh
VMRUN="/Applications/VMware Fusion.app/Contents/Library/vmrun"
VMS=$("${VMRUN}" list | tail -n +2)
if [ "$VMS" == "" ]; then
echo "Need to running the VM"
exit
fi
PS3='Which VM? '
select vm in "${VMS[@]}"
do
break
done
MAC_ADDRESS=$("${VMRUN}" readVariable "$vm" runtimeConfig ethernet0.generatedAddress)
NETWORK=$(grep -oE '192\.168\.\d{1,3}' /Library/Preferences/VMware\ Fusion/vmnet8/dhcpd.conf | head -n 1)
grep ${MAC_ADDRESS} "/Library/Preferences/VMware Fusion/vmnet8/dhcpd.conf" > /dev/null
if [ $? == "0" ]; then
echo "VM Already Configured"
exit;
fi
echo "Changing settings for mac ${MAC_ADDRESS} on ${NETWORK}"
echo "Port on host machine? "
read SOURCE
echo "Port on guest machine? "
read GUEST
HOST=50
grep "${NETWORK}.${HOST}" "/Library/Preferences/VMware Fusion/vmnet8/dhcpd.conf" > /dev/null
while [ $? == "0" ]
do
HOST=$(($HOST + 1))
done
sed -i .bak "/\[incomingtcp\]/ a\
$SOURCE = ${NETWORK}.${HOST}:$GUEST\
" /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf
cat <<DHCPOUTPUT >> "/Library/Preferences/VMware Fusion/vmnet8/dhcpd.conf"
host myvm_$HOST {
hardware ethernet $MAC_ADDRESS;
fixed-address ${NETWORK}.${HOST};
}
DHCPOUTPUT
echo "Fixed address assigned: ${NETWORK}.${HOST}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment