Created
April 8, 2009 09:39
-
-
Save antonlindstrom/91700 to your computer and use it in GitHub Desktop.
School assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# | |
# Program for adding one user. | |
# | |
# Need to have username as input, rest is suggested by program. | |
# Output is a user in the *nix system. | |
# Print program start. | |
print "Add new user\n"; | |
# Subroutine for printing title, suggestion and taking input. | |
sub subr { | |
($text,$suggestion)=(@_); | |
print "$text [$suggestion]: "; | |
chomp($temp=<STDIN>); | |
if(!$temp) { $temp = $suggestion; } | |
return $temp; | |
} | |
# Randomize password. | |
@chars = ('a'..'z','A'..'Z','0'..'9'); | |
$chalen = scalar(@chars); | |
# Length of password, between 8 and 20. | |
$passlen = (int(rand 12)+8); | |
# Seed the random. | |
srand; | |
# Go through $passlen times and add a random char each time. | |
for (0..$passlen) { | |
$_rand = int(rand $chalen); | |
$randpass .= $chars[$_rand]; | |
} | |
# Subroutines with titles and suggestions. | |
while (!$user) { $user = subr("Username", ""); } | |
$pass = subr("Password", $randpass); | |
$uid = subr("UID (negative values use system's suggestion)", "-1"); | |
$homedir = "/home/$user"; | |
$hod = subr("Home directory", $homedir); | |
$skel = subr("Initialize home from skeleton", "y"); | |
$com = subr("Comment on user", ""); | |
$groups = "users,audio,video,games"; | |
$grp = subr("Group list (comma separated, first one is initial group)", $groups); | |
$shl = subr("Shell", "/bin/bash"); | |
# Controls. | |
int($uid); | |
# Format comment correctly. | |
if ($com) {$comment = "-c \"$com\" ";} else { $comment = ""; } | |
# Set skel. | |
if ($skel =~ /y/) {$skel = "-m";} else {$skel = "";} | |
# Format UID | |
if ($uid < "0") {$uid = "";} else {$uid = "-u $uid";} | |
# Get primary group. | |
@grps = split(",", $grp); | |
$primary = shift(@grps); | |
# If user only writes one group | |
unless (scalar(@grps) == 0) { | |
$secgroups = join(",", @grps); | |
$secgroups = "-G $secgroups"; | |
} else { | |
$secgroups = ""; | |
} | |
$command = "useradd -p $pass -d $hod $skel $uid $comment-g $primary $secgroups -s $shl $user"; | |
print "\n$command\n"; | |
print "Is this OK? [y/N] "; | |
chomp($confirm=<STDIN>); | |
if($confirm !~ /y/) { | |
print "Aborting.\n"; | |
exit; | |
} | |
system($command); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment