summaryrefslogtreecommitdiff
path: root/worker/tests/smtp.rb
blob: 66da545bcaa10b0ffdb123d3e1b300074610fa0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'timeout'


#
# Run an SMTP test.
#
#
# Return value
#   TRUE:  The host is up
#
#  FALSE:  The host is not up
#
def smtp_test ( params )

  #
  #  Get the hostname & port to test against.
  #
  host = params['target_host']
  port = 25

  puts "SMTP testing host #{host}:#{port}"


  begin
    timeout(3) do

      begin
        socket = TCPSocket.new( host, port )
        socket.puts( "QUIT\n\n")

        banner = socket.gets(nil)
        banner = banner[0,20]

        socket.close()

        if ( banner =~ /SMTP/i )
          puts "SMTP alive: #{banner}"
          return true
        end
      rescue
        puts "SMTP exception on host #{host}:#{port} - #{$!}"
        return false
      end
    end
  rescue Timeout::Error => e

    puts "SMTP TIMEOUT: #{e}"
    return false
  end
  puts "SMTP Misc Failure"
  return false
end