How to create a ssh tunnel in ruby and then connect to mysql server on the remote host

前端 未结 5 1398
有刺的猬
有刺的猬 2020-12-12 20:56

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:

5条回答
  •  伪装坚强ぢ
    2020-12-12 21:47

    This might be one possible solution:

    require 'rubygems'  
    require 'mysql'  
    require 'net/ssh/gateway'  
    
    
    gateway = Net::SSH::Gateway.new("server","user")  
    port = gateway.open("127.0.0.1",3306,3307)
    
      child = fork do  
        mysql = Mysql.connect("127.0.0.1","user","password","mysql",port)  
        sql = "select sleep(5)"  
        mysql.query(sql)  
        mysql.close  
        exit  
      end  
      puts "child: #{child}"  
    Process.wait  
    gateway.close(port)  
    

    Maybe there is a better way, but this works for what I was trying to do.

提交回复
热议问题