Guest User

Untitled

a guest
Mar 5th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # Program for adding one user.
  4. #
  5. # Need to have username as input, rest is suggested by program.
  6. # Output is a user in the *nix system.
  7.  
  8. # Print program start.
  9. print "Add new user\n";
  10.  
  11. # Subroutine for printing title, suggestion and taking input.
  12. sub subr {
  13. ($text,$suggestion)=(@_);
  14. print "$text [$suggestion]: ";
  15. chomp($temp=<STDIN>);
  16. if(!$temp) { $temp = $suggestion; }
  17. return $temp;
  18. }
  19.  
  20. # Randomize password.
  21. @chars = ('a'..'z','A'..'Z','0'..'9');
  22. $chalen = scalar(@chars);
  23. # Length of password, between 8 and 20.
  24. $passlen = (int(rand 12)+8);
  25. # Seed the random.
  26. srand;
  27. # Go through $passlen times and add a random char each time.
  28. for (0..$passlen) {
  29. $_rand = int(rand $chalen);
  30. $randpass .= $chars[$_rand];
  31. }
  32.  
  33. # Subroutines with titles and suggestions.
  34. while (!$user) { $user = subr("Username", ""); }
  35. $pass = subr("Password", $randpass);
  36. $uid = subr("UID (negative values use system's suggestion)", "-1");
  37. $homedir = "/home/$user";
  38. $hod = subr("Home directory", $homedir);
  39. $skel = subr("Initialize home from skeleton", "y");
  40. $com = subr("Comment on user", "");
  41. $groups = "users,audio,video,games";
  42. $grp = subr("Group list (comma separated, first one is initial group)", $groups);
  43. $shl = subr("Shell", "/bin/bash");
  44.  
  45. # Controls.
  46. int($uid);
  47.  
  48. # Format comment correctly.
  49. if ($com) {$comment = "-c \"$com\" ";} else { $comment = ""; }
  50. # Set skel.
  51. if ($skel =~ /y/) {$skel = "-m";} else {$skel = "";}
  52. # Format UID
  53. if ($uid < "0") {$uid = "";} else {$uid = "-u $uid";}
  54.  
  55. # Get primary group.
  56. @grps = split(",", $grp);
  57. $primary = shift(@grps);
  58. # If user only writes one group
  59. unless (scalar(@grps) == 0) {
  60. $secgroups = join(",", @grps);
  61. $secgroups = "-G $secgroups";
  62. } else {
  63. $secgroups = "";
  64. }
  65.  
  66. $command = "useradd -p $pass -d $hod $skel $uid $comment-g $primary $secgroups -s $shl $user";
  67.  
  68. print "\n$command\n";
  69. print "Is this OK? [y/N] ";
  70. chomp($confirm=<STDIN>);
  71.  
  72. if($confirm !~ /y/) {
  73. print "Aborting.\n";
  74. exit;
  75. }
  76.  
  77. system($command);
Add Comment
Please, Sign In to add comment