使用Perl生成随机密码
使用Perl编写脚本,通过命令行参数控制密码的字符种类(大小写字母、数字、特殊符号)和长度,确保每种指定类型至少出现一次,并支持唯一字符模式避免重复,默认密码长度为9。
生成一个随机密码,听起来简单,但真要满足特定规则——比如必须包含大写字母、小写字母、数字,甚至特殊符号,还得保证不重复——手动拼凑往往容易出错。其实,用Perl写一个灵活的参数化脚本,就能一次性搞定这些需求。下面这个脚本就做到了:通过命令行参数自由控制密码的字符种类和长度,还可以开启“唯一字符”模式,避免重复。

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
sub show_help {
print "Useage:n";
print "newp -aAnsln";
print "-att the password contains lower case letters(a-z)n";
print "-Att the password contains upper case letters(A-Z)n";
print "-ntt the password contains numerical character(0-9)n";
print "-stt the password contains special symbolsn";
print "-utt the password contains only unique charactersn";
print "-l lengtht set the password length(default: 6)n";
exit 0;
}
sub show_version {
print "Version: 0.2.1 Changed the default option: -l 9 -Ana. 2016-4-15n";
exit 0;
}
### main program
use vars qw($opt_a $opt_A $opt_h $opt_l $opt_n $opt_s $opt_u $opt_v);
getopts('aAhl:nsuv');
&show_version if $opt_v;
&show_help if $opt_h;
my $len = $opt_l || 9; # default length 9
my $opt_cnt = 0;
my @rand_str = ();
# store all the characters
my @num = qw(0 1 2 3 4 5 6 7 8 9);
my @ABC = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
my @abc = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z);
# my @sym = qw(! " $ % & ' * + - . / : ; < = > ? @ [ ] ^ _ ` { | } ~);
my @sym = qw(! $ % & * + - . / : ; < = > ? @ [ ] ^ _ ` { | } ~); # no " '
unshift (@sym, '(', ')', '#', ','); # to prevent perl's complains or warnings.
my @all_sym = (@num, @ABC, @abc, @sym);
my @ch_src = ();
if ((!$opt_a) && (!$opt_A) && (!$opt_n) && (!$opt_s)) {
$opt_a++;
$opt_A++;
$opt_n++;
}
if ($opt_a) {
$opt_cnt++;
my $i = rand @abc;
unshift @rand_str, $abc[$i];
if ($opt_u) {
if ($i>=1) {
$abc[$i-1] = shift @abc;
} else {
shift @abc;
}
}
unshift (@ch_src, @abc);
}
if ($opt_A) {
$opt_cnt++;
my $i = rand @ABC;
unshift @rand_str, $ABC[$i];
if ($opt_u) {
if ($i>=1) {
$ABC[$i-1] = shift @ABC;
} else {
shift @ABC;
}
}
unshift (@ch_src, @ABC);
}
if ($opt_n) {
$opt_cnt++;
my $i = rand @num;
unshift @rand_str, $num[$i];
if ($opt_u) {
if ($i>=1) {
$num[$i-1] = shift @num;
} else {
shift @num;
}
}
unshift (@ch_src, @num);
}
if ($opt_s) {
$opt_cnt++;
my $i = rand @sym;
unshift @rand_str, $sym[$i];
if ($opt_u) {
if ($i>=1) {
$sym[$i-1] = shift @sym;
} else {
shift @sym;
}
}
unshift (@ch_src, @sym);
}
if ($len < $opt_cnt) {
print "The count of characters[$len] should not be smaller " .
"than count of character types[$opt_cnt].n";
exit -1;
}
if ($opt_u && $len > (@ch_src + @rand_str)) {
print "The total number of characters[".(@ch_src + @rand_str).
"] which could be contained " .
"in password is smaller than the length[$len] of it.n";
exit -1;
}
foreach (1..$len-$opt_cnt) {
my $i = rand @ch_src;
unshift @rand_str, $ch_src[$i];
if ($opt_u) {
if ($i>=1) {
$ch_src[$i-1] = shift @ch_src;
} else {
shift @ch_src;
}
}
}
foreach (1..$len) {
my $i = rand @rand_str;
print $rand_str[$i];
if ($i>=1) {
$rand_str[$i-1] = shift @rand_str;
} else {
shift @rand_str;
}
}
print "n";
exit 0;
这段脚本的核心逻辑其实很清晰:先根据参数(-a小写、-A大写、-n数字、-s特殊符号)从对应字符池中随机抽取一个作为“保底”,确保每种要求的字符类型至少出现一次;剩下的长度再从所有已启用的字符池中随机填充。如果开启了-u唯一字符模式,每次抽取后还会从原池中移除该字符,避免重复。默认密码长度为9,当然你也可以通过-l随意调整。有了这个脚本,无论是生成临时登录密码,还是批量创建安全密钥,都能一键搞定。
Ollama 可在自己的电脑上下载和运行语言模型,并通过聊天界面、命令行或本地 API 使用。支持 macOS、Windows 与 Linux,模型大小、内存和上下文设置会影响运行体验,也提供独立的云模型入口。
GitHub Desktop 把 Git 仓库的改动对比、提交、分支切换和同步放进桌面窗口,适合不想频繁输入命令的开发者。支持 Windows 与 macOS,可连接 GitHub 仓库并配合编辑器完成日常协作。
Antinote 是 Mac 上的轻量草稿板,支持快捷唤出、清单、计算、单位换算和图片文字识别,也能把内容转到其他笔记工具。要求 macOS 14 以上,适合临时记事和桌面工作中的快速整理。
Ollama 可在自己的电脑上下载和运行语言模型,并通过聊天界面、命令行或本地 API 使用。支持 macOS、Windows 与 Linux,模型大小、内存和上下文设置会影响运行体验,也提供独立的云模型入口。
GitHub Desktop 把 Git 仓库的改动对比、提交、分支切换和同步放进桌面窗口,适合不想频繁输入命令的开发者。支持 Windows 与 macOS,可连接 GitHub 仓库并配合编辑器完成日常协作。
Golden 8 是 Benthic Software 的 Windows Oracle 数据库客户端,提供多标签 SQL 编辑、关联查询辅助、结果表格与导出。连接前需要安装与软件位数一致的 Oracle Client,可先使用 30 天试用版。













