lyadmin/perl-script/create-user.pl
2020-11-26 06:53:20 +00:00

72 lines
1.4 KiB
Perl

#!/usr/bin/perl
use warnings;
use strict;
my $WORKING_DIR = "/home/gashapwn/lyadmin/";
my $ACCOUNT_DIR = "test/";
my $FULL_PATH = "$WORKING_DIR$ACCOUNT_DIR";
my $SHELL_ENUM = {
"SHELL_BASH" => "/usr/local/bin/bash",
"SHELL_KSH" => "/bin/ksh"
};
my @g;
sub create($){
my $id = $_[0];
my $fn1 = $FULL_PATH.$id.".ident";
my $fn2 = $FULL_PATH.$id.".pub";
my $username;
my $shell_pref;
my $user_email;
open FILE, $fn1 or die "could not open file";
$username = <FILE>;
chomp $username;
$user_email = <FILE>;
chomp $user_email;
{
my $shell_var = <FILE>;
chomp $shell_var;
$shell_pref = $SHELL_ENUM->{$shell_var};
}
if(length($username) > 31 || !($username =~ /^[A-Za-z][A-Za-z0-9]+$/)){
printf("%s has an INVALID username\n", $id);
die ("oh no");
}
{
my $cmd;
$cmd = "useradd -m -s " . $shell_pref . " " . $username;
printf("Y/N is this command OK?: %s\n", $cmd);
if(<STDIN> ne "Y\n"){
die "invalid characters?!!";
}
system($cmd);
system("cat $FULL_PATH/$id.pub > /home/$username/.ssh/authorized_keys");
system("chmod 711 /home/$username");
system("rm $FULL_PATH/$id.ident");
system("rm $FULL_PATH/$id.pub");
system("echo $username >> user_list.txt");
}
close FILE;
}
@g = glob("$FULL_PATH*");
@g = map { s/.*\/([^\/]*).pub$/$1/; $_ } grep {$_ =~ /pub$/} @g;
for my $fn (@g){
create($fn);
}