Skip to content

Instantly share code, notes, and snippets.

@ichi
Created March 8, 2013 09:39
Show Gist options
  • Save ichi/5115321 to your computer and use it in GitHub Desktop.
Save ichi/5115321 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
forwardings = { # sample
hoge: 'hoge.com -L 58010/10.120.1.10/3306',
fuga: 'fuga.org -L 58011/localhost/3306',
unko: 'unko.com -L 58012/222.222.222.222/3306'
}
forwardings.each do |k, v|
puts "* #{k}"
puts " #{v}"
exists = `ps aux | grep "#{v.split(/\s+/).last}$"`
`kill -9 #{exists.split(/\s+/)[1]}` unless exists.empty?
fork do
exec <<-CMD
ssh -N \
-o ControlMaster=auto \
-o ExitOnForwardFailure=yes \
-o ConnectTimeout=10 \
-o NumberOfPasswordPrompts=3 \
-o TCPKeepAlive=no \
-o ServerAliveInterval=60 \
-o ServerAliveCountMax=1 \
#{v}
CMD
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment