ruby语法可以链接远程主机么

Python016

ruby语法可以链接远程主机么,第1张

通过gem导入相关库

gem install net-ssh

通过ruby脚本链接远程服务器

代码例子

require 'net/ssh'

host = "xxx.xxx.xxx.xxx"

username = "XXXX"

password = "XXXXXX"

# 通过start方法链接到远程主机

session = Net::SSH.start(host,username,:password => password) do |ssh|

# 输入当前路径

  result1 = ssh.exec("pwd")

  puts result1

# 输出XXX路径下文件

  result2 = ssh.exec!("cd /home/viewlog/qa/XXXls")

  puts result2

end

在使用Cookie.find等操作的时候,就会连接到database.yml中monitor_spider配置的数据库上操作。以前一直都这么用,没发现什么不妥。最近一个项目,由于启动的进程比较多,老是碰到数据库连接池链接获取超时的错误。

通过MySQL Client用命令:show processlist发现数据库连接数量一直居高不下,轻轻松松就上2k+的连接。通过读Rails框架的connection_pool.rb文件代码,发现在各模型中用establish_connection连接数据库会造成很大的问题。文件中类ConnectionHandler的establish_connection方法代码如下:Ruby代码 def establish_connection(name, spec) @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec) end def establish_connection(name, spec)

@connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)