gpt4 book ai didi

linux - Perl - 自动将用户名和密码添加到 Linux 系统的脚本

转载 作者:行者123 更新时间:2023-12-04 19:32:10 25 4
gpt4 key购买 nike

再会!
我只是想帮助解决一个问题。我在 CentOS Linux 命令行中创建了students.txt 列表,如下所示:

said:123456
taha:456789

我在有脚本的地方创建了 perl 文件:
#!/usr/bin/perl
$filename = 'students.txt';
open(FILE, $filename);
while(<FILE>)
{
chomp;
($user, $passcode) = split(/:/, $_);
system "useradd $user -m -p $passcode";
}
close FILE;
print "Users are created successfully :) \n";

运行 perl 文件后,它添加的是用户名,而不是密码。我也是 Perl 的新手。先感谢您。

最佳答案

只需包含 chpasswd 命令。

#!/usr/bin/perl

$filename = 'students.txt';
open(FILE, $filename) or die $!;
while($line = <FILE>) {
chomp $line;
($user, $passcode) = split(/:/, $line);
system "useradd $user -m";
}
close FILE;
system "chpasswd < students.txt";
print "Users are created successfully :) \n";

关于linux - Perl - 自动将用户名和密码添加到 Linux 系统的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29638514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com