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

时间:2015-04-14 22:31:44

标签: linux perl centos system-administration

美好的一天! 我只想帮助解决一个问题。我在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的新手。提前谢谢。

1 个答案:

答案 0 :(得分:0)

只需包含 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";