speedguide 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #! /usr/bin/env ruby
  2. require 'async'
  3. require 'async/http/internet'
  4. require 'nokogiri'
  5. require 'io/console'
  6. class String
  7. def sanitize
  8. self.dump[1...-1]
  9. end
  10. end
  11. Async do
  12. async_internet = Async::HTTP::Internet.new
  13. raise 'Missing port as argument.' unless ARGV.length == 1
  14. port = ARGV[0].to_i.to_s
  15. puts "Fetch information about port #{port}."
  16. query_url = "https://www.speedguide.net/port.php?port=#{port}"
  17. result = async_internet.get(query_url)
  18. raise 'Failed to download port information.' unless result.success?
  19. parser = Nokogiri::HTML5.parse(result.read)
  20. port_entries = parser.css('table.port tr')
  21. raise 'No ports found.' if port_entries.empty?
  22. columns = IO.console.winsize[1]
  23. puts '=' * columns
  24. parser.css('table.port tr')[1..-1].each do |ele|
  25. port = ele.children[1].text.strip
  26. protocol = ele.children[3].text.strip
  27. service = ele.children[5].text.strip
  28. details = ele.children[7].text.strip #.gsub!("\n", '; ')
  29. source = ele.children[9].text.strip
  30. puts "Port(s): #{port.sanitize}"
  31. puts "Protocol: #{protocol.sanitize}"
  32. puts "Service: #{service.sanitize}"
  33. puts "Details: #{details.sanitize.gsub('\n', "\n")}"
  34. puts "Source: #{source.sanitize}"
  35. puts
  36. puts '-' * columns
  37. puts
  38. end
  39. puts "Source: #{query_url}"
  40. puts '=' * columns
  41. ensure
  42. async_internet&.close
  43. end