Skip to content

Instantly share code, notes, and snippets.

@stevenwaskey
Created August 22, 2017 16:03
Show Gist options
  • Save stevenwaskey/5573a7a3b24ec6606e4ad1aa8cb10187 to your computer and use it in GitHub Desktop.
Save stevenwaskey/5573a7a3b24ec6606e4ad1aa8cb10187 to your computer and use it in GitHub Desktop.
Clears out port collisions for VMWare guests in Vagrant env.
#!/bin/bash
##
# Show the user what we're looking to remove from the network configs.
##
printf "\nWe've located the following entries matching the associated port argument. \n"
grep "add_nat_portfwd" /Library/Preferences/VMware\ Fusion/networking | grep $1
grep $1 /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf
##
# Prompt the user for confirmation.
##
printf "\n"
read -p "Are you sure you would like to remove the entries listed above?" -n 1 -r
printf "\n"
##
# Get jiggy wit it.
##
if [[ $REPLY =~ ^[Yy]$ ]]
then
printf "Deleting ... \n"
# MAX OSX sed commands
# sed -i '' '/$1/d' /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf
# sed -i '' '/$1/d' /Library/Preferences/VMware\ Fusion/networking
# Linux sed commands
sed -i.bak '/$1/d' /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf
sed -i.bak '/$1/d' /Library/Preferences/VMware\ Fusion/networking
else
printf "No files we hurt in the making of this script.\n"
fi
@stevenwaskey
Copy link
Author

Usage:

sudo ./clear-net-ports.sh {faulty_port}

or 

sudo ./clear-net-ports.sh 2222

NOTE
This script is not yet 100% complete.
The sed commands do not parse command line arguments.

Further, it would be nice to be able to use multiple port arguments at the command line.

@catalinbostan
Copy link

for MAC OSX i have success with this:
used ex instead sed, and add support for resetting VMware networking
https://gist.github.com/catalinbostan/58940f61851c6493d8f71f3bbc55fbbc

@stevenwaskey
Copy link
Author

Thx @catalinbostan !
I'll give that a shot. I'm usually working in OSX myself when I experience the issue.

@stevenwaskey
Copy link
Author

@catalinbostan
Your script for OSX worked beautifully... Thx!

@umamiMike
Copy link

grep is hanging for me...i just get a usage: grep command just after We've located the following entries matching the associated port argument.

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