rails 实现ping功能

##只要传所需参数就可以进行ping
def ping   
    host = params['hostname'] || 'www.sina.com.cn'
    callback = params['jsoncallback'] || params['callback'] # for cross domain ajax jsonp solution
    count = params['count'] || 4
    file_size = params['file_size'] ||
    ping_cmd = is_windows ? 'ping' : 'ping -c 4'

    out = ''

    IO.popen("#{ping_cmd} #{host} -n #{count} -l #{file_size}") { |f|
      while f.gets do
        out << $_
      end
    }
   
    out.gsub!("\n", "<br />")
   
    if callback
      render :text => { utput => out}.to_json
    else
      render :text => out
    end
end

你可能感兴趣的:(Rails)