610-create-intnet
#!/bin/bash
#
# Create DHCP Network for Internal Network
# Generates network configuration on Guest
#
if [ "$UID" -eq 0 ]
  then echo "This script should NOT be run as root."
  exit 1
fi
#
if [ $# -ne 0 ]; then
  echo "Usage for $0 : No argument required. One time setup"
  exit 1
fi
#
# Setup Internal Network IP Range
VB_VM_INTNET_IP="10.2.2."
# Sample static IP address for a VM
VB_VM_INTNET_IP_STATIC="10"
# If using nic1, then it usually translates to eth0 on guest
VB_VM_INTNET_ETH_INT="eth0"
#
VB_VM_INTNET_IPNW="${VB_VM_INTNET_IP}0"
VB_VM_INTNET_IPBC="${VB_VM_INTNET_IP}255"
VB_VM_INTNET_IPGW="${VB_VM_INTNET_IP}1"
# The dot 100 to 199 is for the DHCP range while the rest is for Static IP
VB_VM_INTNET_IPLO="${VB_VM_INTNET_IP}100"
VB_VM_INTNET_IPHI="${VB_VM_INTNET_IP}199"
VB_VM_INTNET_IPST="${VB_VM_INTNET_IP}${VB_VM_INTNET_IP_STATIC}"
#
echo "Creating Internal Network DHCP. 
This needs to be done only once (not per VM)"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
# Add DHCP for Internal Network
#
echo "Creating DHCP Server for intnet ..."
VBoxManage dhcpserver add --netname intnet \
 --ip ${VB_VM_INTNET_IPGW} --netmask 255.255.255.0 \
 --lowerip ${VB_VM_INTNET_IPLO} --upperip ${VB_VM_INTNET_IPHI} --enable
echo "Listing VirtualBox DHCP Servers ..."
VBoxManage list dhcpservers
#
echo
# Create a Internal Network Adapter - this goes to eth0 if using nic1
echo "Run below command to create Internal Network on Guest on eth0:"
echo "VBoxManage modifyvm <VM> --nic1 intnet"
echo
echo "Edit file: /etc/network/interfaces in Guest 
to configure network in Debian/Ubuntu:
"
echo "For DHCP configure network settings as below:"
echo "# For VM Internal Network"
echo "auto eth0
iface eth0 inet dhcp
"
echo "For configuring Static IP address use the settings below:"
echo "# Change 'address' as required"
echo "# For VM Internal Network"
echo "auto ${VB_VM_INTNET_ETH_INT}"
echo "iface ${VB_VM_INTNET_ETH_INT} inet static"
echo "        address ${VB_VM_INTNET_IPST}"
echo "        netmask 255.255.255.0"
# Do NOT specify gateway if you have another network for gateway
#echo "        gateway ${VB_VM_INTNET_IPGW}"
echo "        network ${VB_VM_INTNET_IPNW}"
echo "        broadcast ${VB_VM_INTNET_IPBC}"
echo
#
echo "To remove DHCP Server run:
VBoxManage dhcpserver remove --netname intnet
"
#echo "Powering VM on ..."
#VBoxManage startvm ${VB_VM_NAME} --type headless
#
exit