1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#!/usr/bin/ruby -w
#
# $Id: pwgen,v 1.3 2004/09/04 22:20:27 ianmacd Exp $
#
# Copyright (C) 2004 Ian Macdonald
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
require 'optparse'
require 'ostruct'
require 'password'
class Optparse
USAGE_BANNER = "Usage: pwgen [ OPTIONS ] [ password_length ] [ number_passwords ]"
def self.parse(args)
options = OpenStruct.new
options.columns = $stdout.tty?
options.one_case = $stdout.tty? ? Password::ONE_CASE : 0
options.one_digit = $stdout.tty? ? Password::ONE_DIGIT : 0
options.secure = false
opts = OptionParser.new do |opts|
opts.banner = USAGE_BANNER
opts.on( "-C", "Print the generated passwords in columns") do
options.columns = true
end
opts.on( "-1", "Don't print the generated passwords",
" in columns") do
options.columns = false
end
opts.on( "-c", "--[no-]capitalise", "--[no-]capitalize",
"Include at least one capital letter in",
" the password" ) do |opt|
options.one_case = opt ? Password::ONE_CASE : 0
end
opts.on( "-n", "--[no-]numerals",
"Include at least one digit in the password" ) do |opt|
options.one_digit = opt ? Password::ONE_DIGIT : 0
end
opts.on( "-s", "--secure", "Generate completely random passwords" ) do
options.secure = true
end
opts.on( "-v", "--version", "Display version and exit" ) do
puts PWGEN_VERSION
exit
end
opts.on_tail( "-h", "--help", "Display this usage message and exit" ) do
puts opts
exit
end
end
opts.parse!(args)
options
end
end
PWGEN_VERSION = '0.5.2'
TERM_WIDTH = 80
options = Optparse.parse(ARGV)
unless [ 0, 2 ].include? ARGV.size
puts Optparse::USAGE_BANNER
exit 1
end
length, number = *ARGV.map { |arg| arg.to_i }
length ||= 8
generator = if length < 5 || options.secure
Proc.new { Password.random( length ) }
else
Proc.new { Password.phonemic( length, options.one_case |
options.one_digit ) }
end
columns = options.columns ? TERM_WIDTH / ( length + 1 ) : 1
columns = 1 if columns == 0
number ||= options.columns ? columns * 20 : 1
need_new_line = false
0.upto number - 1 do |n|
if ! options.columns || n % columns == columns - 1
puts generator.call
need_new_line = false
else
print generator.call, ' '
need_new_line = true
end
end
puts if need_new_line
|