Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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);
Add Comment
Please, Sign In to add comment