#!/usr/bin/env ruby # $Id$ # $URL$ require "getoptlong" def parse_options @options = {} begin opts = GetoptLong.new( [ "-c", GetoptLong::REQUIRED_ARGUMENT ], [ "-l", GetoptLong::REQUIRED_ARGUMENT ], [ "-s", GetoptLong::REQUIRED_ARGUMENT ], [ "-h", GetoptLong::NO_ARGUMENT ] ) opts.quiet=true opts.each do |opt, arg| @options[opt] = arg end if @options["-h"] usage end rescue GetoptLong::InvalidOption print "#{$!}\n" usage end # default values if @options["-s"].nil? @options["-s"] = "s" end if @options["-l"].nil? @options["-l"] = 8 end if @options["-c"].nil? @options["-c"] = 8 end return @options end def usage print <] [-l ] [-h] [-s ] -c generate this many passwords (default: 8) -l generate passwords of this length (default: 8) -s use character set for [S]MS, [P]aper, [T]elephone (default: s) -h this help message XXX exit end def random_string(len) rand_set_max = @rand_chars.size rand_max = @rand_chars[0].size ret = "" lastpos = -1 pos = -1 len.times{ while pos == lastpos pos = rand(rand_max) end lastpos = pos char = @rand_chars[rand(rand_set_max)][pos] if @options["-s"] == "s" && char.chr == '*' spec_max = @spec_chars.size char = @spec_chars[rand(spec_max)] end ret << char } ret end @rand_chars = Array.new @spec_chars = "" parse_options case @options["-s"] # SMS Chars. Available with 1 keypress. when "s" @rand_chars = [ "ADGJMPTW*", "adgjmptw*", "23456789*" ] @spec_chars = ",-?!@:;/()" # Paper chars. No l1I, 0O, g9 when "p" @rand_chars = [ "ABCDEFGHJKMNPQRSTUVWXYZabcefhjkmnpqrstuvwxyz2345678!@$%^&*" ] # Telephone class. No mn, akh and none of the paper chars stuff. when "t" @rand_chars = [ "BCDEFGJPQRSTUVWXYZbcefjpqrstuvwxyz2345678!@$%^&*" ] else puts "Charater set '#{@options["-s"]}' unknown" exit end (1 .. @options["-c"].to_i).each { puts random_string(@options["-l"].to_i) }