Skip to content

Instantly share code, notes, and snippets.

@renfredxh
Created July 9, 2014 15:25
Show Gist options
  • Save renfredxh/e392c67b3196a37b1230 to your computer and use it in GitHub Desktop.
Save renfredxh/e392c67b3196a37b1230 to your computer and use it in GitHub Desktop.
Forward multiple ports from localhost to Vagrant
#!/usr/bin/env ruby
# Description: Forwards one or more ports from your localhost to a Vagrant VM
# via ssh. Useful for viewing multiple dev applications in Vagrant at once.
#
# Installation:
# $ mv vforward.rb /usr/bin/vforward
# $ chmod +x /usr/bin/vforward
#
# Usage: vforward [ports ...]
#
# Example: vforward 3000 4000
def catch_interrupts
begin
yield
rescue Interrupt
exit
end
end
ports = $* || [3000]
ports.each do |port|
fork do
puts "Forwarding port #{port} to Vagrant"
catch_interrupts do
system("vagrant ssh -- -N -L #{port}:localhost:#{port}")
end
end
end
puts "Ctrl-C to stop forwarding"
catch_interrupts do
Process.waitall
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment