12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #! /usr/bin/env ruby
- require 'async'
- require 'async/http/internet'
- require 'nokogiri'
- require 'io/console'
- class String
- def sanitize
- self.dump[1...-1]
- end
- end
- Async do
- async_internet = Async::HTTP::Internet.new
- raise 'Missing port as argument.' unless ARGV.length == 1
- port = ARGV[0].to_i.to_s
- puts "Fetch information about port #{port}."
- query_url = "https://www.speedguide.net/port.php?port=#{port}"
- result = async_internet.get(query_url)
- raise 'Failed to download port information.' unless result.success?
-
- parser = Nokogiri::HTML5.parse(result.read)
- port_entries = parser.css('table.port tr')
- raise 'No ports found.' if port_entries.empty?
- columns = IO.console.winsize[1]
- puts '=' * columns
- parser.css('table.port tr')[1..-1].each do |ele|
- port = ele.children[1].text.strip
- protocol = ele.children[3].text.strip
- service = ele.children[5].text.strip
- details = ele.children[7].text.strip #.gsub!("\n", '; ')
- source = ele.children[9].text.strip
-
- puts "Port(s): #{port.sanitize}"
- puts "Protocol: #{protocol.sanitize}"
- puts "Service: #{service.sanitize}"
- puts "Details: #{details.sanitize.gsub('\n', "\n")}"
- puts "Source: #{source.sanitize}"
- puts
- puts '-' * columns
- puts
- end
- puts "Source: #{query_url}"
- puts '=' * columns
- ensure
- async_internet&.close
- end
|