Skip to content

Instantly share code, notes, and snippets.

@paulsturgess
Created February 2, 2012 16:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulsturgess/1724551 to your computer and use it in GitHub Desktop.
Save paulsturgess/1724551 to your computer and use it in GitHub Desktop.
Run a MySQL command on a remote server (via SSH) using Ruby
#!/usr/bin/env ruby
require 'rubygems'
require 'mysql'
require 'net/ssh/gateway'
HOST = ''
SSH_USER = ''
SSH_PASS = ''
DB_HOST = "127.0.0.1"
DB_USER = ""
DB_PASS = ""
DB_NAME = ""
FINISH = Time.gm(2012, 01, 01, 12, 00)
gateway = Net::SSH::Gateway.new(HOST, SSH_USER, :password => SSH_PASS)
port = gateway.open(DB_HOST,3306,3307)
child = fork do
mysql = Mysql.connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, port)
sql = ""
while Time.now < FINISH do
mysql.query(sql)
puts "."
sleep 0.25
end
mysql.close
exit
end
puts "child: #{child}"
Process.wait
gateway.close(port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment