#!/usr/bin/ruby # Tue May 24 16:02:59 EDT 2011 # vbatts@hashbangbash.com # # simple server to spin up a bunch of listen ports # # require 'gserver' class PortServer < GServer def initialize(port = rand(65354), *args) super(port,*args) end def serve(io) io.puts("#{self.port} @ #{Time.now.to_s}") end end if __FILE__ == $PROGRAM_NAME #require 'thread' require 'optparse' servers = [] options = {:num => 5, :host => "127.0.0.1" } OptionParser.new {|opts| opts.banner = "simple utility to spin up listening ports" opts.separator("") opts.on("-n ","Number of ports to open, default is #{options[:num]}") {|o| options[:num] = o.to_i } opts.on("-a ","which host to use, default is #{options[:host]}") {|o| options[:host] = o } }.parse! begin for i in (0..options[:num]) s = PortServer.new(rand(65354), host = options[:host]) s.audit = true s.start printf("Server started with port: %d\n", s.port) end printf("now i'm sleeping!\n") printf("^c to kill\n") while true sleep(0.5) end rescue Interrupt printf("killed! i am out!\n") ensure begin for server in servers server.shutdown end rescue end end end