#!/usr/bin/ruby ################################################################################################################## # # runningserver.rb # 12/DEC/2006 # Daniel Martin-Gomez # # Desc: # Script to create a connection on the specified port to check if a server is # listening on it. # # Version: # v1.0 [12/Dec/2006]: first released # ################################################################################################################### $help =<] [ ...] Options: -h/-help/--help: This help message -p : Specify which TCP port should be tested. Arguments: host(s): The different hosts to test. EOH #------------------------------ Input argument parsing if ( ARGV.include?('--help') || ARGV.include?('-help') || ARGV.include?('-h') || (ARGV.size==0) ) then puts puts $help exit end # set the port number if ARGV.include?('-p') then p_position = ARGV.index('-p') # read value after the -p if ARGV[p_position+1] != nil then $port = ARGV[p_position+1].to_i else puts "ERROR: you kind of need to specify the port if you use the -p parameter" puts "\t e.g: -p 8080\n\n" exit end # clear these values in the array ARGV[p_position] = nil ARGV[p_position+1] = nil end # clear the ARGV of already parsed ARGs hostlist = ARGV.compact #------------------------------ Interesting stuff starts here require 'socket' require 'net/http' require 'timeout' if $port == nil then $port=18264 end #for each host in the command line hostlist.each() do |host| puts serverstr = "No server was found in port #{$port}" begin Timeout::timeout(3) do client = TCPSocket.open(host, $port) client.close serverstr = "Server is running on port #{$port}" end rescue Exception => e serverstr = "No server was found in port #{$port}" end puts "#{host}: #{serverstr}" end puts