Created
June 25, 2010 19:36
-
-
Save chetan/453339 to your computer and use it in GitHub Desktop.
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/ruby | |
# | |
# adduser - Unix-like adduser script. Prompts for parameters and uses dscl internally to create | |
# new NetInfo users and groups. | |
# | |
# Chetan Sarva <[email protected]> | |
def next_user_id(start = 700) | |
ids = `dscl . -list /Users UniqueID`.split("\n").map{|s| s =~ /(\d+)$/; $1 }.sort.uniq | |
n = start | |
while ids.include? n.to_s do | |
n += 1 | |
end | |
n | |
end | |
def next_group_id(start = 700) | |
ids = `dscl . -list /Groups PrimaryGroupID`.split("\n").map{|s| s =~ /(\d+)$/; $1 }.sort.uniq | |
n = start | |
while ids.include? n.to_s do | |
n += 1 | |
end | |
n | |
end | |
puts "adduser!" | |
puts | |
print "username: " | |
username = STDIN.gets | |
username.strip! | |
if username.empty? then | |
puts "must enter a username!" | |
exit | |
end | |
dusername = "_#{username}" | |
print "home dir [default=/var/empty]: " | |
home = STDIN.gets | |
home = "/var/empty" if home.strip.empty? | |
home.strip! | |
password = "*" | |
def_group_id = next_group_id() | |
print "group id (default=#{def_group_id}): " | |
group_id = STDIN.gets | |
group_id = def_group_id if group_id.strip.empty? | |
group_id.strip! if group_id.kind_of? String | |
def_user_id = next_user_id() | |
print "user id (default=#{def_user_id}): " | |
user_id = STDIN.gets | |
user_id = def_user_id if user_id.strip.empty? | |
user_id.strip! if user_id.kind_of? String | |
print "real name (default=daemon): " | |
realname = STDIN.gets | |
realname = "daemon" if realname.strip.empty? | |
realname.strip! | |
print "shell: (default=/usr/bin/false): " | |
shell = STDIN.gets | |
shell = "/usr/bin/false" if shell.strip.empty? | |
shell.strip! | |
cmd = "sudo dscl localhost -create /Local/Default/Groups/#{dusername}" | |
system("#{cmd}") | |
system("#{cmd} PrimaryGroupID #{group_id}") | |
system("sudo dscl localhost -append /Local/Default/Groups/#{dusername} RecordName #{username}") | |
cmd = "sudo dscl localhost -create /Local/Default/Users/#{dusername}" | |
system("#{cmd}") | |
system("#{cmd} UniqueID #{user_id}") | |
system("#{cmd} PrimaryGroupID #{group_id}") | |
system("#{cmd} UserShell #{shell}") | |
system("#{cmd} RealName '#{realname}'") | |
system("#{cmd} NFSHomeDirectory #{home}") | |
system("#{cmd} Password '#{password}'") | |
system("sudo dscl localhost -append /Local/Default/Users/#{dusername} RecordName #{username}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment