ruby Teste de ping

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Teste de ping相关的知识,希望对你有一定的参考价值。

require 'resolv'
require 'net/smtp'
require 'logger'
require 'benchmark'

class PrintLog
  @@logger = Logger.new('email_ping.log')

  def self.log_message(message, with_separator=false)
    separator = ''
    separator = "#{ '-' * 50 }\n" if with_separator

    @@logger << "#{ message }\n#{ separator }"
    puts "#{ message }\n#{ separator }"
  end
end

class DomainList
  @@white_list = {}
  @@black_list = []

  def self.black_list
    @@black_list
  end

  def self.white_list
    @@white_list
  end

  def self.add_white_list(key, value)
    if @@white_list.key?(key.to_sym)
      PrintLog.log_message "- Using #{ key } from domains white list"
    else
      @@white_list[key.to_sym] = value
      PrintLog.log_message "- Adding #{ key } to domains white list"
    end
  end

  def self.black_list=(value)
    @@black_list << value
  end
end

class EmailPing
  DOMAIN_LIST = %w(yahoo.com hotmail.com gmail.com msn.com bananananana.net homtail.com bol.com.br abazucagigante.as clickjogos.com.br) # ig.com.br, aol.com currently not accepting SMTP connections
  NAME_LIST = %w(josias asdas andre vid4lok4 carol mirian_19 ncxvmnxc android_ios baena gabriel rafinha055 josue234r willianjose lsdkfn kar boom)
  SALT_LIST = %w(silva petini asrmy 57 2 abril naruto robinho 2007 _ .)

  attr_accessor :address, :domain
  attr_reader :mx_records

  def initialize
    @address = pick_email_address
    @domain = @address.split('@').last
  end

  def pick_email_address
    "#{ [NAME_LIST.sample, NAME_LIST.sample + SALT_LIST.sample, NAME_LIST.sample + SALT_LIST.sample + NAME_LIST.sample].sample }@#{ DOMAIN_LIST.sample }"
  end

  def ping
    mx_server = mx_records.sample.exchange.to_s
    PrintLog.log_message "- Connecting to #{ mx_server }..."

    Net::SMTP.start mx_server, 25 do |smtp|
      smtp.helo "gmail.com"
      smtp.mailfrom ['guilhermeteodoro.tester@gmail.com', 'gui_teo@hotmail.com'].sample

      PrintLog.log_message "- Pinging #{ address }..."

      m = Benchmark.measure do
        begin
          smtp.rcptto address
          PrintLog.log_message "\n===> Address probably exists."
        rescue => e
          PrintLog.log_message "\n===> [X] Address probably doesn't exist."
          PrintLog.log_message "      - CAUSE: #{ e }"
        end
      end

      PrintLog.log_message "#{ m }"
    end
  end

  def resolv_mx_records
    @mx_records = if DomainList.white_list.keys.include?(domain)
      DomainList.white_list[domain.to_sym]
    else
      Resolv::DNS.new.getresources domain, Resolv::DNS::Resource::IN::MX
    end

    self
  end

  def invalid?
    return false if @mx_records.any?
    DomainList.black_list = domain
    PrintLog.log_message "- #{ domain } added to black list"
    PrintLog.log_message "\n===> [X] No MX records for #{ domain }. Skipping it.", true
    true
  end
end

n = ARGV[0] ? ARGV[0].chomp.to_i : 10

total = Benchmark.measure {
  n.times do |i|
    email_ping = EmailPing.new

    PrintLog.log_message "TEST #{ i + 1 }\n"

    if DomainList.black_list.include?(email_ping.domain)
      PrintLog.log_message "===> [X] #{ email_ping.domain } is blacklisted. Skipping it.", true
      next
    end

    PrintLog.log_message "Test with: #{ email_ping.address }"
    PrintLog.log_message "- Resolving MX records for: #{ email_ping.domain }"

    next if email_ping.resolv_mx_records.invalid?

    DomainList.add_white_list email_ping.domain, email_ping.mx_records.map{ |mx| mx.exchange.to_s }

    begin
      tries ||= 1
      email_ping.ping
    rescue
      return unless (tries += 1) > 3
      PrintLog.log_message "--- Ooops, tring again for #{ email_ping.domain }, attempt #{ tries } of 3 ---"
      retry
    end

      PrintLog.log_message '', true
  end
}

PrintLog.log_message "********** TOTAL TIME ***********\n#{ total }"

以上是关于ruby Teste de ping的主要内容,如果未能解决你的问题,请参考以下文章

ruby Ruby:Suma de digitos al cubo

ruby 电子邮件ping.rb

ruby Códigodelnivel 8,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 9,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 7,modo Intermediate de Ruby Warrior

ruby Códigodelnivel 6,modo Intermediate de Ruby Warrior