我想创建多个用户并生成密码并将其打印为文本文件,如何在 shell 脚本中编写?(例如:创建 5 个用户,密码长度为 10)
答案1
以下脚本执行您要求的操作:
#!/usr/bin/perl
my $user="user";
my $encrypted;
my $userno=1;
my $userstogenerate=6;
sub encryptingPsw {
my $psw = shift;
my $count = shift;
my @salt = ('.', '/', 'a'..'z', 'A'..'Z', '0'..'9');
my $salt = "";
$salt.= $salt[rand(63)] foreach(1..$count);
crypt($psw, $salt);
}
for ($userno;$userno<$userstogenerate;$userno++) {
print "User name $userno: ";
chomp (my $user = <STDIN>);
$encrypted = &encryptingPsw('passwd', 4);
print "sudo useradd -p '$encrypted' $user\n";
@array = `echo "Username: " $user "Password: " $encrypted >> users.txt`;
}
@array = `cat users.txt | lpr`;
新用户及其密码写在users.txt
文件中。如果您在机器上连接了打印机,则会users.txt
打印该文件。如果没有,则会出现错误。