Created
February 2, 2012 16:55
-
-
Save paulsturgess/1724551 to your computer and use it in GitHub Desktop.
Run a MySQL command on a remote server (via SSH) using Ruby
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 | |
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