#!/usr/bin/env ruby require 'optparse' require 'portchecker' # -------------------------------------------------------------------------------- # # Check port # # -------------------------------------------------------------------------------- # # This function will take the address and port and check to see if they are open. # # -------------------------------------------------------------------------------- # def check_port(options) if Portchecker::Check.port_open?(options[:address], options[:port]) puts "#{options[:address]} port #{options[:port]} is open" else puts "#{options[:address]} port #{options[:port]} is closed" end end # -------------------------------------------------------------------------------- # # Process Arguments # # -------------------------------------------------------------------------------- # # This function will process the input from the command line and work out what it # # is that the user wants to see. # # # # This is the main processing function where all the processing logic is handled. # # -------------------------------------------------------------------------------- # def process_arguments options = {} # Enforce the presence of mandatory = %I[address port] optparse = OptionParser.new do |opts| opts.banner = "Usage: #{$PROGRAM_NAME}" opts.on('-h', '--help', 'Display this screen') do puts opts exit(1) end opts.on('-a', '--address string', 'The address to connect to') do |address| options[:address] = address end opts.on('-p', '--port integer', 'The port to connect to') do |port| options[:port] = port end end begin optparse.parse! options[:message] = ARGF.read if !STDIN.tty? # override message parameter if data is piped in missing = mandatory.select { |param| options[param].nil? } raise OptionParser::MissingArgument.new(missing.join(', ')) unless missing.empty? rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e puts e.to_s puts optparse exit end exit 0 if check_port(options) exit 1 end # -------------------------------------------------------------------------------- # # Main() # # -------------------------------------------------------------------------------- # # The main function where all of the heavy lifting and script config is done. # # -------------------------------------------------------------------------------- # def main process_arguments end main # -------------------------------------------------------------------------------- # # End of Script # # -------------------------------------------------------------------------------- # # This is the end - nothing more to see here. # # -------------------------------------------------------------------------------- #