diff options
| author | Steve Kemp <steve@steve.org.uk> | 2012-11-22 15:38:07 +0000 | 
|---|---|---|
| committer | Steve Kemp <steve@steve.org.uk> | 2012-11-22 15:38:07 +0000 | 
| commit | 918d964adee64d0a14fc80797def1070c39061b3 (patch) | |
| tree | 25662cfd3ca6e73855d16ef8937f0ba2ff97603e /lib/custodian/protocol-tests | |
| parent | c3a3f054ada31db25db4194ecb4a17db7fdb1c47 (diff) | |
  Parse the configuration file into arrays of jobs, via our test-factory
Diffstat (limited to 'lib/custodian/protocol-tests')
| -rwxr-xr-x | lib/custodian/protocol-tests/dns.rb | 211 | ||||
| -rwxr-xr-x | lib/custodian/protocol-tests/ftp.rb | 149 | ||||
| -rwxr-xr-x | lib/custodian/protocol-tests/http.rb | 144 | ||||
| -rwxr-xr-x | lib/custodian/protocol-tests/jabber.rb | 151 | ||||
| -rwxr-xr-x | lib/custodian/protocol-tests/ldap.rb | 141 | ||||
| -rwxr-xr-x | lib/custodian/protocol-tests/ping.rb | 134 | ||||
| -rwxr-xr-x | lib/custodian/protocol-tests/rsync.rb | 151 | ||||
| -rwxr-xr-x | lib/custodian/protocol-tests/smtp.rb | 150 | ||||
| -rwxr-xr-x | lib/custodian/protocol-tests/ssh.rb | 149 | ||||
| -rwxr-xr-x | lib/custodian/protocol-tests/tcp.rb | 168 | 
10 files changed, 0 insertions, 1548 deletions
diff --git a/lib/custodian/protocol-tests/dns.rb b/lib/custodian/protocol-tests/dns.rb deleted file mode 100755 index 3d9f9fd..0000000 --- a/lib/custodian/protocol-tests/dns.rb +++ /dev/null @@ -1,211 +0,0 @@ -#!/usr/bin/ruby1.8 - - -require 'resolv-replace' - - -# -#  This is a protocol tester for DNS. -# -class DNSTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - -  # -  # The error text we return on failure. -  # -  attr_reader :error - - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -    @error     = nil - -    # -    # Ensure we have a host to monitor -    # -    if ( @test_data["target_host"].nil? ) -      @error = "Missing host to test against." -      raise ArgumentError, @error -    end - -    # -    #  Ensure we have test-specific keys. -    # -    if ( @test_data["resolve_name"].nil? ) -      @error = "Missing host to resolve." -      raise ArgumentError, @error -    end -    if ( @test_data["resolve_type"].nil? ) -      @error = "Missing type to resolve, for example A/MX/NS." -      raise ArgumentError, @error -    end -    if ( @test_data["resolve_expected"].nil? ) -      @error = "Missing expected results to compare against.." -      raise ArgumentError, @error -    end -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error()". -  # -  def run_test - -    # -    # Reset state from previous test. -    # -    @error = nil - -    # -    # Get the nameserver to resolve with -    # -    nameserver = @test_data["target_host"] - -    # -    # Get the record type to lookup -    # -    record = @test_data["resolve_type"] - -    # -    # Get the hostname to lookup -    # -    lookup = @test_data["resolve_name"] - -    # -    # Get the expected results -    # -    expected = @test_data["resolve_expected"] - -    # -    # Do the lookup abort if that fails. -    # -    timeout = @test_data["timeout"].to_i - -    results = resolve_via( nameserver, record, lookup, timeout ) -    return false if ( results.nil? ) - - -    # -    # Show the results if we found something. -    # -    if ( @test_data['verbose'] ) -      results.each do |res| -        puts "Result: #{res}" -      end -    end - -    # -    # OK we have an array of results.  If every one of the expected -    # results is contained in those results returned then we're good. -    # -    expected.split( /;/ ).each do |required| -      if ( ! results.include?( required ) ) -        @error = "The expected result #{required} was not found in the results: #{results.join(",")}" -        return false -      end -    end - -    return true -  end - - -  # -  # Resolve an IP -  # -  def resolve_via( server, ltype, name, period ) -    puts "Resolving #{name} [#{ltype}] via server #{server}" - -    results = Array.new() - -    begin -      timeout( period ) do - -        begin -          Resolv::DNS.open(:nameserver=>[server]) do |dns| -            case ltype -            when /^A$/ then -              dns.getresources(name, Resolv::DNS::Resource::IN::A).map{ |r| results.push r.address.to_s() } -            when /^AAAA$/ then -              dns.getresources(name, Resolv::DNS::Resource::IN::AAAA).map{ |r| results.push r.address.to_s() } - -            when /^NS$/ then -              dns.getresources(name, Resolv::DNS::Resource::IN::NS).map{ |r| results.push Resolv.getaddresses(r.name.to_s()) } - -            when /^MX$/ then -              dns.getresources(name, Resolv::DNS::Resource::IN::MX).map{ |r| results.push Resolv.getaddresses(r.exchange.to_s()) } -            end -          end -        rescue Exception => x -          @error = "Exception was received when resolving : #{x}" -          return nil -        end -      end -    rescue Timeout::Error => e -      @error = "Timed-out connecting #{e}" -      return nil -    end -    results.flatten! -    return results -  end - - -  # -  #  Return the error text for why this test failed. -  # -  def error -    return @error -  end - - - - -end - - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host"      => "a.ns.bytemark.co.uk", -    "test_type"        => "dns", -    "verbose"          => 1, -    "timeout"          => "4", -    "test_alert"       => "DNS failure", -    "resolve_name"     => "support.bytemark.co.uk", -    "resolve_type"     => "MX", -    "resolve_expected" => "89.16.184.148;89.16.184.149;89.16.184.150" -  } - - -  # -  #  Run the test. -  # -  obj = DNSTest.new( test ) -  if ( obj.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts obj.error() -  end - -end diff --git a/lib/custodian/protocol-tests/ftp.rb b/lib/custodian/protocol-tests/ftp.rb deleted file mode 100755 index 1ecd68a..0000000 --- a/lib/custodian/protocol-tests/ftp.rb +++ /dev/null @@ -1,149 +0,0 @@ -#!/usr/bin/ruby1.8 - - -require 'timeout' -require 'socket' - -# -# This class is responsible for performing tests against a remote FTP server -# -# -class FTPTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - -  # -  # The error text we return on failure. -  # -  attr_reader :error - - - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -    @error     = nil - -    # -    # Ensure we have a host to probe -    # -    if ( @test_data["target_host"].nil? ) -      @error = "Missing target for the test." -      raise ArgumentError, @error -    end - -    # -    # Ensure we have a port to test. -    # -    if ( @test_data["test_port"].nil? ) -      @error = "Missing port for the test." -      raise ArgumentError, @error -    end - -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error()". -  # -  def run_test - -    # -    # Reset state from previous test. -    # -    @error = nil - -    # -    #  Get the hostname & port to test against. -    # -    host = @test_data["target_host"] -    port = @test_data["test_port"] - -    puts "FTP testing host #{host}:#{port}" if ( @test_data['verbose'] ) - -    begin -      timeout( @test_data["timeout"].to_i ) do - -        begin -          socket = TCPSocket.new( host, port ) -          socket.puts( "QUIT") - -          banner = socket.gets(nil) -          banner = banner[0,20] unless( banner.nil? ) - -          socket.close() - -          if ( ( !banner.nil? ) && ( banner =~ /^220/ ) ) -            return true -          else -            @error = "Banner didn't report OK: #{banner}" -          end -        rescue -          @error = "FTP exception on host #{host}:#{port} - #{$!}" -          return false -        end -      end -    rescue Timeout::Error => e -      @error = "Timed-out connecting #{e}" -      return false -    end -    @error = "Misc. failure." -    return false -  end - - -  # -  #  Return the error. -  # -  def error -    return @error -  end - -end - - - - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host" => "mirror.bytemark.co.uk", -    "test_type"   => "ftp", -    "test_port"   => 21, -    "verbose"     => 1, -    "timeout"     => 4, -    "test_alert"  => "The FTP server no worky", -  } - - -  # -  #  Run the test. -  # -  tst = FTPTest.new( test ) -  if ( tst.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts tst.error() -  end - -end diff --git a/lib/custodian/protocol-tests/http.rb b/lib/custodian/protocol-tests/http.rb deleted file mode 100755 index 102ffea..0000000 --- a/lib/custodian/protocol-tests/http.rb +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/ruby1.8 - - -require 'custodian/webfetch' - - -class HTTPTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -    @error     = nil - -    # -    # Ensure we have an URL -    # -    if ( @test_data["target_host"].nil? ) -      @error = "Missing URL for the test." -      raise ArgumentError, @error -    end - -    # -    # Ensure we have a port -    # -    if ( @test_data["test_port"].nil? ) -      @error = "Missing port for the test." -      raise ArgumentError, @error -    end - -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error()". -  # -  def run_test - -    # -    # Reset state from previous test. -    # -    @error = nil - - -    # -    # Run the fetch. -    # -    obj = WebFetch.new( @test_data["target_host"], @test_data["timeout"].to_i ) - -    # -    # If we succeeded in the fetch -    # -    if ( obj.fetch() ) - -      # -      #  Do we need to test for a HTTP status code? -      # -      if ( @test_data["http_status"] ) - -        puts "Testing for HTTP status code: #{@test_data['http_status']}"  if ( @test_data['verbose'] ) - -        if ( obj.status().to_i != @test_data['http_status'].to_i) -          @error = "#{@error} <p>The HTTP status-code was '#{obj.status}' not '#{@test_data['http_status']}'.</p>" -        end -      end - -      # -      #  Do we need to search for text in the body of the reply? -      # -      if ( @test_data['http_text'] ) -        puts "Testing for text in the response: '#{@test_data['http_text']}'" if ( @test_data['verbose'] ) - -        if (! obj.content.match(/#{@test_data['http_text']}/i) ) -          @error = "#{@error}<p>The response did not contain our expected text '#{test_data['http_text']}'</p>" -        end -      end - -      return true if ( @error.nil? ) - -      return false -    end - -    @error = obj.error() -    return false -  end - - -  # -  #  Return the error text for why this test failed. -  # -  def error -    return @error -  end - - -end - - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host" => "http://itsdanbull.co.uk/", -    "test_type"   => "http", -    "verbose"     => 1, -    "timeout"     => 3, -    "test_port"   => 80, -    "test_alert"  => "Collector is unavailable", -    "http_status" => "200", -    "http_text"   => "Dan Bull" -  } - - -  # -  #  Run the test. -  # -  http = HTTPTest.new( test ) -  if ( http.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts http.error() -  end - -end diff --git a/lib/custodian/protocol-tests/jabber.rb b/lib/custodian/protocol-tests/jabber.rb deleted file mode 100755 index a35ef28..0000000 --- a/lib/custodian/protocol-tests/jabber.rb +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from a Jabber server that looks -# reasonable. -# -class JABBERTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - -  # -  # The error text we return on failure. -  # -  attr_reader :error - - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -    @error     = nil - -    # -    # Ensure we have a host to probe -    # -    if ( @test_data["target_host"].nil? ) -      @error = "Missing target for the test." -      raise ArgumentError, @error -    end - -    # -    # Ensure we have a port to test. -    # -    if ( @test_data["test_port"].nil? ) -      @error = "Missing port for the test." -      raise ArgumentError, @error -    end - -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error()". -  # -  def run_test - -    # -    # Reset state from previous test. -    # -    @error = nil - -    # -    #  Get the hostname & port to test against. -    # -    host = @test_data['target_host'] -    port = @test_data['test_port'] - -    puts "Jabber testing host #{host}:#{port}" if ( @test_data['verbose'] ) - -    begin -      timeout(@test_data["timeout"].to_i) do - -        begin -          socket = TCPSocket.new( host, port ) -          socket.puts( "QUIT") - -          banner = socket.gets(nil) -          banner = banner[0,20] unless( banner.nil? ) - -          socket.close() - -          if ( ( !banner.nil? ) &&  ( banner =~ /xml version/i ) ) -            puts "Jabber alive: #{banner}" if ( @test_data['verbose'] ) -            return true -          else -            @error = "Banner didn't seem reasonable: #{banner}" -            return false; -          end -        rescue -          @error = "Jabber exception on host #{host}:#{port} - #{$!}" -          return false -        end -      end -    rescue Timeout::Error => e -      @error = "TIMEOUT: #{e}" -      return false -    end - -    @error = "Misc failure" -    return false -  end - - - -  # -  #  Return the error text for why this test failed. -  # -  def error -    return @error -  end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host" => "chat.bytemark.co.uk", -    "test_type"   => "jabber", -    "test_port"   => "5222", -    "timeout"     => 4, -    "verbose"     => 1, -    "test_alert"  => "Chat is down?", -  } - - -  # -  #  Run the test. -  # -  obj = JABBERTest.new( test ) -  if ( obj.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts obj.error() -  end - -end diff --git a/lib/custodian/protocol-tests/ldap.rb b/lib/custodian/protocol-tests/ldap.rb deleted file mode 100755 index af0addc..0000000 --- a/lib/custodian/protocol-tests/ldap.rb +++ /dev/null @@ -1,141 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from an LDAP server. -# -class LDAPTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - -  # -  # The error text we return on failure. -  # -  attr_reader :error - - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -    @error     = nil - - -    # -    # Ensure we have a host to probe -    # -    if ( @test_data["target_host"].nil? ) -      @error = "Missing target for the test." -      raise ArgumentError, @error -    end - -    # -    # Ensure we have a port to test. -    # -    if ( @test_data["test_port"].nil? ) -      @error = "Missing port for the test." -      raise ArgumentError, @error -    end -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error()". -  # -  def run_test - -    # -    # Reset state from previous test. -    # -    @error = nil - -    # -    #  Get the hostname & port to test against. -    # -    host = @test_data['target_host'] -    port = @test_data['test_port'] - -    puts "LDAP testing host #{host}:#{port}" if ( @test_data['verbose'] ) - -    begin -      timeout(@test_data["timeout"].to_i) do - -        begin -          socket = TCPSocket.new( host, port ) -          socket.puts( "QUIT") -          socket.close() - -          puts "LDAP alive" if ( @test_data['verbose'] ) -          return true -        rescue -          @error = "Exception connecting to host #{host}:#{port} - #{$!}" -          return false -        end -      end -    rescue Timeout::Error => e -      @error = "TIMEOUT: #{e}" -      return false -    end - -    @error = "Misc failure" -    return false -  end - - - -  # -  #  Return the error text for why this test failed. -  # -  def error -    return @error -  end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host" => "auth.bytemark.co.uk", -    "test_type"   => "ldap", -    "test_port"   => 389, -    "verbose"     => 1, -    "timeout"     => 5, -    "test_alert"  => "LDAP is down?", -  } - - -  # -  #  Run the test. -  # -  obj = LDAPTest.new( test ) -  if ( obj.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts obj.error() -  end - -end diff --git a/lib/custodian/protocol-tests/ping.rb b/lib/custodian/protocol-tests/ping.rb deleted file mode 100755 index 309c77b..0000000 --- a/lib/custodian/protocol-tests/ping.rb +++ /dev/null @@ -1,134 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a ping response from the remote host. -# -class PINGTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - -  # -  # The error text we return on failure. -  # -  attr_reader :error - - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error()". -  # -  def run_test - -    # -    # Reset state from previous test. -    # -    @error = nil - - -    # -    # Find the binary we're going to invoke. -    # -    binary = nil -    binary = "/usr/bin/multi-ping"  if ( File.exists?( "/usr/bin/multi-ping" ) ) - -    if ( binary.nil? ) -      @error = "Failed to find 'multi-ping'" -      return false -    end - - -    # -    #  Get the hostname to test against. -    # -    host = @test_data['target_host'] - - -    # -    # Sanity check the hostname for ping-tests, to -    # avoid this security hole: -    # -    #   $(/tmp/exploit.sh) must run ping .. -    # -    raise ArgumentError, "Invalid hostname for ping-test: #{host}" unless( host =~ /^([a-zA-Z0-9:\-\.]+)$/ ) - - - -    # -    # Show the hostname. -    # -    puts "ping testing host #{host}" if ( @test_data['verbose'] ) - - -    if ( system( "#{binary} #{host}" ) == true ) -      puts "PING OK" if ( @test_data['verbose'] ) -      return true -    else -      @error = "Ping failed.  TODO: Mtr" -      return false -    end - -  end - - -  # -  #  Return the error text for why this test failed. -  # -  def error() -    return @error -  end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host" => "upload.ns.bytemark.co.uk", -    "test_type"   => "ping", -    "verbose"     => 1, -    "timeout"     => 5, -    "test_alert"  => "Pingly faily", -  } - - -  # -  #  Run the test. -  # -  obj = PINGTest.new( test ) -  if ( obj.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts obj.error() -  end - -end diff --git a/lib/custodian/protocol-tests/rsync.rb b/lib/custodian/protocol-tests/rsync.rb deleted file mode 100755 index 4493d0f..0000000 --- a/lib/custodian/protocol-tests/rsync.rb +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from an rsync server that looks -# reasonable. -# -class RSYNCTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - -  # -  # The error text we return on failure. -  # -  attr_reader :error - - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -    @error     = nil - -    # -    # Ensure we have a host to probe -    # -    if ( @test_data["target_host"].nil? ) -      @error = "Missing target for the test." -      raise ArgumentError, @error -    end - -    # -    # Ensure we have a port to test. -    # -    if ( @test_data["test_port"].nil? ) -      @error = "Missing port for the test." -      raise ArgumentError, @error -    end -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error()". -  # -  def run_test - -    # -    # Reset state from previous test. -    # -    @error = nil - -    # -    #  Get the hostname & port to test against. -    # -    host = @test_data['target_host'] -    port = @test_data['test_port'] - -    puts "rsync testing host #{host}:#{port}" if ( @test_data['verbose'] ) - -    begin -      timeout(@test_data["timeout"].to_i) do - -        begin -          socket = TCPSocket.new( host, port ) -          socket.puts( "QUIT") - -          banner = socket.gets(nil) -          banner = banner[0,20] unless( banner.nil? ) - -          socket.close() - -          if ( ( !banner.nil? ) && ( banner =~ /rsyncd/i ) ) -            puts "rsync alive: #{banner}" if ( @test_data['verbose'] ) -            return true -          else -            @error = "Banner didn't seem reasonable: #{banner}" -            return false; -          end -        rescue -          @error = "rsync exception on host #{host}:#{port} - #{$!}" -          return false -        end -      end -    rescue Timeout::Error => e -      @error = "TIMEOUT: #{e}" -      return false -    end - -    @error = "Misc failure" -    return false -  end - - - -  # -  #  Return the error text for why this test failed. -  # -  def error -    return @error -  end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host" => "upload.ns.bytemark.co.uk", -    "test_type"   => "rsync", -    "test_port"   => "873", -    "verbose"     => 1, -    "timeout"     => 5, -    "test_alert"  => "DNS upload service failure", -  } - - -  # -  #  Run the test. -  # -  obj = RSYNCTest.new( test ) -  if ( obj.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts obj.error() -  end - -end - diff --git a/lib/custodian/protocol-tests/smtp.rb b/lib/custodian/protocol-tests/smtp.rb deleted file mode 100755 index 5da672f..0000000 --- a/lib/custodian/protocol-tests/smtp.rb +++ /dev/null @@ -1,150 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from an SMTP server that looks -# reasonable. -# -class SMTPTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - -  # -  # The error text we return on failure. -  # -  attr_reader :error - - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -    @error     = nil - - -    # -    # Ensure we have a host to probe -    # -    if ( @test_data["target_host"].nil? ) -      @error = "Missing target for the test." -      raise ArgumentError, @error -    end - -    # -    # Ensure we have a port to test. -    # -    if ( @test_data["test_port"].nil? ) -      @error = "Missing port for the test." -      raise ArgumentError, @error -    end -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error". -  # -  def run_test -    # -    # Reset state from previous test. -    # -    @error = nil - -    # -    #  Get the hostname & port to test against. -    # -    host = @test_data['target_host'] -    port = @test_data['test_port'] - -    puts "SMTP testing host #{host}:#{port}" if ( @test_data['verbose'] ) - -    begin -      timeout(@test_data["timeout"].to_i) do - -        begin -          socket = TCPSocket.new( host, port ) -          socket.puts( "QUIT") - -          banner = socket.gets(nil) -          banner = banner[0,40] unless( banner.nil? ) - -          socket.close() - -          if ( ( !banner.nil? ) && ( banner =~ /SMTP/i ) ) -            puts "SMTP alive: #{banner}" if ( @test_data['verbose'] ) -            return true -          else -            @error = "Banner didn't seem reasonable: #{banner}" -            return false; -          end -        rescue -          @error = "SMTP exception on host #{host}:#{port} - #{$!}" -          return false -        end -      end -    rescue Timeout::Error => e -      @error = "TIMEOUT: #{e}" -      return false -    end - -    @error = "Misc failure" -    return false -  end - - - -  # -  #  Return the error text for why this test failed. -  # -  def error -    return @error -  end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host" => "mail.steve.org.uk", -    "test_type"   => "smtp", -    "test_port"   => "25", -    "verbose"     => 1, -    "timeout"     => 5, -    "test_alert"  => "SMTP failure", -  } - - -  # -  #  Run the test. -  # -  obj = SMTPTest.new( test ) -  if ( obj.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts obj.error() -  end - -end diff --git a/lib/custodian/protocol-tests/ssh.rb b/lib/custodian/protocol-tests/ssh.rb deleted file mode 100755 index 4ee9974..0000000 --- a/lib/custodian/protocol-tests/ssh.rb +++ /dev/null @@ -1,149 +0,0 @@ -#!/usr/bin/ruby1.8 - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from an SSH server that looks -# reasonable. -# -class SSHTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - -  # -  # The error text we return on failure. -  # -  attr_reader :error - - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -    @error     = nil - - -    # -    # Ensure we have a host to probe -    # -    if ( @test_data["target_host"].nil? ) -      @error = "Missing target for the test." -      raise ArgumentError, @error -    end - -    # -    # Ensure we have a port to test. -    # -    if ( @test_data["test_port"].nil? ) -      @error = "Missing port for the test." -      raise ArgumentError, @error -    end -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error". -  # -  def run_test - -    # -    # Reset state from previous test. -    # -    @error = nil - -    # -    #  Get the hostname & port to test against. -    # -    host = @test_data['target_host'] -    port = @test_data['test_port'] - -    puts "ssh testing host #{host}:#{port}" if ( @test_data['verbose'] ) - -    begin -      timeout(@test_data["timeout"].to_i) do - -        begin -          socket = TCPSocket.new( host, port ) -          socket.puts( "QUIT") - -          banner = socket.gets(nil) -          banner = banner[0,20] unless( banner.nil? ) - -          socket.close() - -          if ( !banner.nil? && ( banner =~ /ssh/i ) ) -            puts "ssh alive: #{banner}" if ( @test_data['verbose'] ) -            return true -          else -            @error = "Banner didn't seem reasonable: #{banner}" -            return false; -          end -        rescue -          @error = "ssh exception on host #{host}:#{port} - #{$!}" -          return false -        end -      end -    rescue Timeout::Error => e -      @error = "TIMEOUT: #{e}" -      return false -    end - -    @error = "Misc failure" -    return false -  end - - - -  # -  #  Return the error text for why this test failed. -  # -  def error -    return @error -  end - -end - - -# -# Sample test, for testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host" => "ssh.steve.org.uk", -    "test_type"   => "ssh", -    "test_port"   => 2222, -    "verbose"     => 1, -    "timeout"     => 5, -    "test_alert"  => "Steve's host isn't running SSH?", -  } - - -  # -  #  Run the test. -  # -  obj = SSHTest.new( test ) -  if ( obj.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts obj.error() -  end - -end diff --git a/lib/custodian/protocol-tests/tcp.rb b/lib/custodian/protocol-tests/tcp.rb deleted file mode 100755 index a6770e4..0000000 --- a/lib/custodian/protocol-tests/tcp.rb +++ /dev/null @@ -1,168 +0,0 @@ -#!/usr/bin/ruby1.8 - - - -require 'socket' -require 'timeout' - - -# -# Test that we can receive a response from a TCP server that matches -# a given banner. -# -class TCPTest - -  # -  # Data passed from the JSON hash. -  # -  attr_reader :test_data - -  # -  # The error text we return on failure. -  # -  attr_reader :error - - - -  # -  # Save the data away. -  # -  def initialize( data ) -    @test_data = data -    @error     = nil - - -    # -    # Ensure we have a host to probe -    # -    if ( @test_data["target_host"].nil? ) -      @error = "Missing target for the test." -      raise ArgumentError, @error -    end - -    # -    # Ensure we have a port to test. -    # -    if ( @test_data["test_port"].nil? ) -      @error = "Missing port for the test." -      raise ArgumentError, @error -    end -  end - - -  # -  # Run the test. -  # -  #  Return "true" on success -  # -  #  Return "false" on failure. -  # -  # If the test fails the details should be retrieved from "error". -  # -  def run_test -    # -    # Reset state from previous test. -    # -    @error = nil - -    # -    #  Get the hostname & port to test against. -    # -    host = @test_data['target_host'] -    port = @test_data['test_port'] - -    # -    #  Get the banner we expect -    # -    banner = @test_data['banner'] - -    puts "TCP testing host #{host}:#{port}" if ( @test_data['verbose'] ) -    if ( @test_data['verbose'] && ( !banner.nil? ) ) -        puts "Looking for banner '#{banner}'" -    end - -    begin -      timeout(@test_data["timeout"].to_i) do - -        begin -          socket = TCPSocket.new( host, port ) -          socket.puts( "QUIT") - -          # read a banner from the remote server -          read = socket.gets(nil) - -          # trim to a sane length & strip newlines. -          read = read[0,255] unless ( read.nil? ) -          read.gsub!(/[\n\r]/, "") unless ( read.nil? ) - -          socket.close() - - -          if ( banner.nil? ) -            return true -          else -            # test for banner -            if ( ( !read.nil? ) && ( read =~ /#{banner}/i ) ) -              puts "We connected and matched the banner against '#{read}'" if ( @test_data['verbose'] ) -              return true -            end - -            @error = "We expected a banner matching '#{banner}' but we got '#{read}'" -            return false -          end -        rescue -          @error = "Exception connecting to host #{host}:#{port} - #{$!}" -          return false -        end -      end -    rescue Timeout::Error => e -      @error = "TIMEOUT: #{e}" -      return false -    end -    @error = "Misc failure" -    return false -  end - - - -  # -  #  Return the error text for why this test failed. -  # -  def error -    return @error -  end - -end - - -# -# Sample test, for basic testing. -# -if __FILE__ == $0 then - -  # -  #  Sample data. -  # -  test = { -    "target_host" => "mail.steve.org.uk", -    "test_type"   => "tcp", -    "test_port"   => "25", -    "banner"      => "SMTP", -    "verbose"     => 1, -    "timeout"     => 5, -    "test_alert"  => "TCP-port failure", -  } - - -  # -  #  Run the test. -  # -  obj = TCPTest.new( test ) -  if ( obj.run_test ) -    puts "TEST OK" -  else -    puts "TEST FAILED" -    puts obj.error() -  end - -end  | 
