从Perl代码使用usseradd时出错:无法锁定/ etc / passwd

时间:2018-12-28 15:29:08

标签: perl unix

我正在编写用于用户管理的perl程序,但不断出现此错误: useradd:权限被拒绝。 useradd:无法锁定/ etc / passwd;稍后再试。 我使用su -c命令运行该程序,下面的代码是相关的代码段-https://github.com/Kajkacz/ASU_Indexing/blob/master/Users.pl上的完整代码。

sub createUserPage{

    $Mw->destroy;
    $Mw = MainWindow->new;
    $Mw->geometry("500x300");
    $Mw->title("Create User");
    my $uid = &getFreeUID;
    my $uspasswd = &randomizePassword;
    my $usname = 'UsernameHere';

    my $username = $Mw->Entry(-textvariable=> \$usname )->grid($Mw->Label(-text => 'Username'));
    my $UID = $Mw->Entry(-textvariable=> \$uid )->grid($Mw->Label(-text => 'UID'));
    my $password = $Mw ->Entry(-textvariable=> \$uspasswd)->grid($Mw->Label(-text => 'Password'));

    $Mw->Button(-text=>"Create User", -command =>sub{&createUser($uid,$usname,$uspasswd)},-width => $buttonWidth)->grid();
    $Mw->Button(-text=>"Check If UID is free", -command =>sub{&checkUID($uid)},-width => $buttonWidth)->grid();
    $Mw->Button(-text=>"Get Random Password", -command =>sub{$uspasswd = &randomizePassword},-width => $buttonWidth)->grid();

    $Mw->Button(-text=>"Back to Main Menu", -command =>sub{&getStartWindow},-width => $buttonWidth)->grid();}

sub createUser{
     my $UID = shift;
     my $username =     shift;
     my $password = shift;
     my $adduser = '/usr/sbin/useradd';
     my $cmd = qq($adduser \"$username\");
     # my $cmd = qq($adduser \"$username\" -p \"$password\" -u $UID);
     print "$cmd \n";
     system $cmd; }

我欢迎任何见识。此外,我有史以来第一个Perl程序,请随时更正我的代码

1 个答案:

答案 0 :(得分:1)

因此,我使用以下解决方案找到了解决方法:How to fix 'sudo: no tty present and no askpass program specified' error? 并将用户添加到无需密码提示即可运行的命令列表中。不确定是hacky还是好的解决方法