0

i am trying to make a little website with Sinatra, where i want to display SNMP data.

require 'sinatra'
#require 'sinatra/reloader'
require 'snmp'
get'/' do
    'Hello World'
    SNMP::Manager.open(:host => 'localhost') do |manager|
        response = manager.get(["sysDescr.0","sysName.0"])
        response.each_varbind do |vb|
            puts "#{vb.name.to_s} #{vb.value.to_s} #{vb.value.asn1_type}"
        end
    end

end

Unfortunately this code outputs the result on the console and not on the Web Page.

I hope you can help me.

1 Answers1

2

It looks like your calling puts as you iterate through your data, this will print the results to the console as ruby cannot input items directly onto the web page, and because puts is only able to print into your console/ terminal. if you want to display the results on your web page you will need to pass them as params in to your :erb file, then display them within the erb file like so:

get'/' do
  'Hello World'
  SNMP::Manager.open(:host => 'localhost') do |manager|
      @response = manager.get(["sysDescr.0","sysName.0"]) # add the @ symbol to then pass as params into the erb file
      end
erb(:index) # load up your erb file
end

then simply load your values in the erb file like so

<%=@response.each_varbind do |vb|%>
<p>
<%={vb.name.to_s} + {vb.value.to_s} + {vb.value.asn1_type}%>
</p>

<%end%>

Now the controller will load the index.html.erb file whenever the route get('/') is called and you should see your values displayed within the paragraph tag on screen

Hope that helps!

Jack Branch
  • 152
  • 8