Created
July 9, 2014 15:25
-
-
Save renfredxh/e392c67b3196a37b1230 to your computer and use it in GitHub Desktop.
Forward multiple ports from localhost to Vagrant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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