使用Perl生成随机密码
更新时间:2020年6月29日 13:42 点击:1625
可以通过参数控制生成密码中包括的字符种类
#!/usr/bin/perl use strict; use warnings; use Getopt::Std; sub show_help { print "Useage:\n"; print "newp -aAnsl\n"; print "-a\t\t the password contains lower case letters(a-z)\n"; print "-A\t\t the password contains upper case letters(A-Z)\n"; print "-n\t\t the password contains numerical character(0-9)\n"; print "-s\t\t the password contains special symbols\n"; print "-u\t\t the password contains only unique characters\n"; print "-l length\t 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-15\n"; 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;
以上就是本文给大家分享的全部代码了,希望对大家学习Perl能够有所帮助
相关文章
- 这两天在用Perl编写一些监控脚本,其实写代码也是一件挺有意思的事情,就是挺废时间的。而且,由于语法不太熟,基本想到一个东西都要先Google一下看怎么实现。...2020-06-29
- 一、简介 模式指在字符串中寻找的特定序列的字符,由反斜线包含:/def/即模式def。其用法如结合函数split将字符串用某模式分成多个单词:@array = split(/ /, $line);二、匹配...2020-06-29
- 下面小编就为大家带来一篇Perl与JS的对比分析(数组、哈希)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-29
- Perl时间函数localtime的使用介绍,这里简单的介绍下,更多请查看官方介绍...2020-06-29
- 一、列表 列表是包含在括号里的一序列的值,可以为任何数值,也可为空,如:(1, 5.3 , "hello" , 2),空列表:()。 注:只含有一个数值的列表(如:(43.2) )与该数值本身(即:43.2 )是不同...2020-06-29
- 这篇文章主要介绍了Perl中的特殊内置变量详细介绍,需要的朋友可以参考下...2020-06-29
- 这篇文章主要介绍了讲Perl中的本地时间与UNIX时间戳间相互转换的方法,主要用到了Perl中的Date::Parse模块,需要的朋友可以参考下...2020-06-29
- 对所有输入文件,如果没有版权信息则加上版权信息,否则什么都不做,并对原文件以.bak结尾备份,需要的朋友可以参考下...2020-06-29
- 这篇文章主要介绍了Perl学习笔记之CPAN使用介绍,本文讲解了什么是CPAN、CPAN的目录作用介绍、CPAN安装Perl Module的两种方法等内容,需要的朋友可以参考下...2020-06-29
- 这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
- 这篇文章主要介绍了Perl AnyEvent中的watcher实例,关于AnyEvent请参阅的更多介绍请参阅文中的相关链接,需要的朋友可以参考下...2020-06-29
- perl获取日期与时间的例子,供大家学习参考...2020-06-29
- 这篇文章主要介绍了Perl中的列表和数组学习笔记,本文讲解了列表、数组--列表的存贮、数组的存取、字符串中的方括号和变量替换、列表范围、数组的输出等内容,需要的朋友可以参考下...2020-06-29
- 这篇文章主要介绍了使用perl实现拆分数据表(mysql)并迁移数据实例,本文提供了3个脚本,分别用于拆分数据表、迁移数据、插入测试数据,需要的朋友可以参考下...2020-06-29
- 写过很多关于Perl编程,今天继续分享一篇利用perl后门实现正向和反向连接的实例代码,需要的朋友可以参考下其中的内容详情...2020-06-29
- 这篇文章主要介绍了7个perl数组高级操作技巧,本文讲解了数组去重、数组合并、查找最大值、列表归并等内容,需要的朋友可以参考下...2020-06-29
perl uc,lc,ucfirst,lcfirst大小写转换函数
这篇文章主要介绍了perl 大小写字母转换函数,需要的朋友可以参考下...2020-06-29- 这篇文章主要介绍了Perl内置特殊变量总结,需要的朋友可以参考下...2020-06-29
- perl 文件操作总结,需要的朋友可以参考下...2020-06-29
- 这篇文章主要介绍了Perl学习笔记之文件操作,本文分别给出了打开文件、读取文件、写入文件代码实例,需要的朋友可以参考下...2020-06-29