Skip to content

Instantly share code, notes, and snippets.

@OwensCode
Created October 29, 2015 23:46
Show Gist options
  • Save OwensCode/184fc0ee149321ffe7f8 to your computer and use it in GitHub Desktop.
Save OwensCode/184fc0ee149321ffe7f8 to your computer and use it in GitHub Desktop.
#!/bin/bash
###############################################################################
# Shell script to help deal with problems with VirtualBox and network routing
# issues when running Docker, mainly caused by VPN software. The symptom is
# "docker-machine env" or "boot2docker env" hanging instead of returning
# environment variables. It might help with Vagrant as well.
#
# Tested on OS/X Yosemite with GNU coreutils installed via Homebrew, which
# might (or might not) affect the operation of the script.
#
# Owen Berry
###############################################################################
# Likely candidate for the network in my experience
VBOX_SUBNET="192.168.99"
# Try to detect the VirtualBox network interface
VBOX_INTERFACE=$(ifconfig | grep -B2 "${VBOX_SUBNET}" | head -n 1 | cut -f 1 -d:)
# And the VirtualBox gateway, which could show as a VPN gateway
ROUTE_GATEWAY=$(route get ${VBOX_SUBNET}.100 | grep gateway | cut -f2 -d:)
# curses stuff
bold=$(tput bold)
nbold=$(tput sgr0)
###################### Functions #####################
function check_for_existing_route {
if [ -z "${ROUTE_GATEWAY}" ]; then
echo
echo "${bold}It looks like there might already be a route for docker:${nbold}"
echo "---------------------------------------------------------"
echo
route get ${VBOX_SUBNET}.100
echo
while true; do
read -r -p "Carry on (y/n)? " yn
case $yn in
[Yy]* ) yn=x; break;;
* ) remove_route_message; exit;;
esac
done
fi
}
function confirm_add_route {
local route_interface
route_interface=$(route get ${VBOX_SUBNET}.100 | grep interface | cut -f2 -d:)
echo
echo "${bold}Possible network interface:${nbold}"
echo "---------------------------"
echo
ifconfig | grep -B2 "${VBOX_SUBNET}"
echo
echo "${bold}Current route interface:${nbold} ${route_interface}"
echo "${bold}Current route gateway:${nbold} ${ROUTE_GATEWAY}"
echo
while true; do
read -r -p "Add route for interface ${VBOX_INTERFACE} (y/n)? " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer y/n.";;
esac
done
}
function add_route {
echo
echo "You may need to enter your password to add the route now"
sudo route add -net ${VBOX_SUBNET}.0/24 -interface "${VBOX_INTERFACE}"
}
function remove_route_message {
echo
echo "${bold}To remove the route:${nbold}"
echo " sudo route delete -net ${VBOX_SUBNET}.0/24 -interface \"${VBOX_INTERFACE}\""
echo
}
###################### Start #####################
check_for_existing_route
confirm_add_route
add_route
remove_route_message
##################### End #####################
@OwensCode
Copy link
Author

Unfortunately, this does not always work with Cisco AnyConnect. Sometimes adding the route seems to have no effect at all, and I'm not sure why right now. Try rebooting. :-/

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