I would like to create a ruby script that I can run mysql commands on a remote server through a ssh tunnel.
Right now I have a manual process to do this:
I've been trying out the gateway code above, one main difference being I have to use ssh keys for passwordless access, but also found the code handing on the Mysql.connect statement. However, when I replaced
mysql = Mysql.connect("127.0.0.1",...)
with
mysql = Mysql.connect("localhost",...)
it worked fine.
My final code looks like this:
require 'rubygems'
require 'mysql'
require 'net/ssh/gateway'
gateway = Net::SSH::Gateway.new('host',
'user',
:keys => ['myprivatekey.pem'],
:verbose => :debug)
port = gateway.open("127.0.0.1",3306,3307)
mysql = Mysql.connect("localhost","dbuser","dbpassword","dbname",3307)
puts "here"
mysql.close
gateway.close(port)
gateway.shutdown!