Sha256: f06a86821957e4bf40aec2623faf19c458bb5ddeac63757e6284bcec877cc082

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true
require 'resolv'
require 'socket'
require 'net/ping/tcp'

module Netchk
  class DNSServerVerifier
    def initialize(**options)
      @resolve_conf = options['resolv.conf']
    end

    def verify
      nameservers = self.nameservers
      if nameservers.empty?
        $stderr.puts 'No DNS server found. Verify your configuration.'
      else
        puts "Using DNS servers #{nameservers.map { |ns| ns.join('#') }.join(', ')}"
        nameservers.map do |ns|
          verify_nameserver(*ns)
        end
      end
    end

    private
      def verify_nameserver(ip, port)
        ping = Net::Ping::TCP.new(ip, port)
        unless ping.ping?
          $stderr.puts "Failed to ping DNS server #{ip}##{port}"
        end
      end

      def nameservers
        # Dirty trick to get default nameserver list from /etc/resolv.conf
        # without parsing the file manually.
        ::Resolv::DNS.open(@resolve_conf) do |dns|
          dns.lazy_initialize
          dns.instance_variable_get('@config').instance_variable_get('@nameserver_port')
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
netchk-0.0.1 lib/netchk/dns_server_verifier.rb