繁体   English   中英

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

[英]Perl - script to automatically add username and password to the Linux system

再会! 我只是想帮助解决一个问题。 我在 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";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM