Skip to content

Instantly share code, notes, and snippets.

@da667
Last active August 24, 2022 10:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save da667/1a71ac4e85867a75785291c53306237f to your computer and use it in GitHub Desktop.
Save da667/1a71ac4e85867a75785291c53306237f to your computer and use it in GitHub Desktop.
This is a shell script for readers of Building Virtual Labs : A Hands-On Guide, or students of the Building Virtual Labs video training. This script will check to see if the interfaces vmnet2 or vboxnet0 exist. If either does, it configures the IP to 172.16.2.0 and the netmask to 255.255.255.0, then sets up a static route to 172.16.2.0/24 via 17…
#!/bin/bash
#This script is meant for VMware Fusion Professional, or Oracle Virtualbox users on OSX
#This script checks for the existence of the interface vmnet2 (vmware fusion) or vboxnet0 (virtualbox)
#and will assign the IP address 172.16.1.2 to the first interface it finds. The script will check to see if vmnet2 exists, then check for vboxnet0
#if neither interface exists, the script will fail.
#after setting the IP address, the script attempts to add a static route to the 172.16.2.0 network via 172.16.1.1 (LAN interface of the pfSense VM)
#Note: If you are using alternative networks for your lab other than 172.16.1.0/24, and 172.16.2.0/24, you will have to modify the route, and ifconfig statements on lines 38, 50, and 69 on your own to reflect your "Management" and "IPS" networks.
### Notifying users to have VMs up and running ###
echo "Warning: Please make sure that all of your vmware fusion OR oracle virtualbox VMs are running. In particular, the pfSense VM MUST be running!"
read -p "Once you have verified that your VMs are up and running, Press enter to continue."
##################################################
### Root privilege check ###
echo "Checking for root privs.."
if [ $(whoami) != "root" ]; then
echo "This script must be ran with sudo or root privileges."
exit 1
else
echo "We are root."
fi
############################
### Check to see if vmnet2 exists. If it does, we set the IP to 172.16.1.2 with a netmask of 255.255.255.0 ###
### If it does not, we check to see if vboxnet0 exists. if it does, we set its IP to 172.16.1.2/24 as well ###
### If neither interface exists, then we inform the user and abort the script. ###############################
echo "Checking to see if vmnet2 exists..."
ifconfig vmnet2 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "vmnet2 interface exists. Setting IP to 172.16.1.2/24..."
ifconfig vmnet2 172.16.1.2 netmask 255.255.255.0
if [ $? -eq 0 ]; then
echo "vmnet2 interface IP set to 172.16.1.2."
else
echo "Could not set IP address of vmnet2 interface. You got troubleshooting to do. Aborting."
exit 1
fi
else
echo "vmnet2 interface does not exist. Checking to see if vboxnet0 exists..."
ifconfig vboxnet0 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "vboxnet0 interface exists. Setting IP to 172.16.1.2/24..."
ifconfig vboxnet0 172.16.1.2 netmask 255.255.255.0
if [ $? -eq 0 ]; then
echo "vboxnet0 interface IP set to 172.16.1.2."
else
echo "Could not set IP address of vboxnet0 interface. You got troubleshooting to do. Aborting."
exit 1
fi
else
echo "vboxnet0 interface does not exist. This script only checks for vmnet2 or vboxnet0. Aborting."
exit 1
fi
fi
#######################################################################################################
### Adding static route ###
echo "Adding static route to 172.16.2.0/24 via 172.16.1.1..."
route add 172.16.2.0/24 172.16.1.1
if [ $? -ne 0 ]; then
echo "Could not create route to 172.16.2.0/24. Does the network exist? Is vmware fusion running? Is virtualbox running? Is the pfSense/gateway VM running? Does the pfSense LAN interface have the correct IP address?"
exit 1
else
echo "added route to 172.16.2.0/24 via 172.16.1.1."
fi
###########################
exit 0
@da667
Copy link
Author

da667 commented Nov 22, 2020

Please be aware that this script assumes your lab network is using the default subnet mask assignments (e.g. management network = 172.16.1.0/24, IPS1+IPS2 segments = 172.16.2.0/24). If you've decided to use a different RFC1918 address range, you'll want adjust lines 38, 50, and 69 at the very least with the correct IP addresses and network ranges your lab is using instead. You might also want to adjust the echo statements at lines 37, 40, 49, 52, 67, 71, and 74 as well.

Please be aware this script assumes that your lab environment (or at least the pfSense virtual machine) is fully configured, and capable of routing traffic between network segments correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment