• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: add users remotely
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: add users remotely


  • Subject: Re: add users remotely
  • From: Sander Tekelenburg <email@hidden>
  • Date: Thu, 25 Jan 2007 23:22:30 +0100

At 22:42 +0530 UTC, on 2007-01-25, Rishi wrote:

> Any ideas where to get an Apple Script to Add users on remote computers?
>
> I have 12 iMac's with Apple Remote Desktop enabled.
>
> I wanted to run an Apple script to add a user called "student" with
> the password set as "student" in all the 12 iMacs
>
> Any ideas how else to go about achieving this?

A couple of years ago I wrote a script that allows you to create new users on
remote machines over ssh. No ARD required. I haven't used it for a long time
so you should double-check the code. IIRC the routine to decide on the right
UID for the new account contains some error. Also, I believe the script
doesn't create the remote user's home directory, probably because there was a
problem doing that right. Lastly, I see some references to Tiger, but the
script originated earlier. So just that some parts are Tiger-aware doesn't
mean all are. But it may give you a starting point:

-- June 07, 2003
-- by Sander Tekelenburg, <email@hidden>
-- optionally uses system.osax

-- known bugs:
-- 1]
-- User's real name contains single quotes due to "quoted form of" need...
-- Can probably be fixed by settting entire do shell script command to single
variable end setting _that_ to "quoted form of".
-- 2]
-- under 10.1: /System/Library/UserTemplate/English.lproj/
-- under 10.2: /System/Library/User\\ Template/English.lproj/

property remoteMac : ""
-- shells we hope to find on the remote machine:
property shellList : {"tcsh", "zsh", "bash"}

display dialog "Create new user?" with icon 1

-- assemble remote host info
set remoteMac to text returned of (display dialog "Enter IP address of the
remote Mac" default answer remoteMac)
try
	-- in try block 'cause otherwise some weird script error sometimes...
	set currUser to my getMyshortname()
on error
	set currUser to ""
end try
set remoteID to text returned of (display dialog "Enter the id with which to
login to " & remoteMac default answer currUser)
try
	-- requires Mac OS X 10.4 ("Tiger")
	set remoteIDPass to text returned of (display dialog "Enter the passphrase
for " & remoteID & "@" & remoteMac default answer "" with hidden answer)
on error
	try
		-- requieres system.osax
		set remoteIDPass to askPassword "Enter the passphrase for " & remoteID
& "@" & remoteMac
	on error
		display dialog "It looks like you are not running Mac OS X 10.4 or up
and don't have system.osax installed. You should only continue if you are
100% sure nobody is looking at your screen!" & return & return & ¬
			"With system.osax this wouldn't be an issue. system.osax is free and
can be downloaded from <http://osaxen.com>" buttons ¬
			{"Continue", "Quit"} default button 2 with icon 2
		set theResult to the result
		if theResult = "Continue" then
			set remoteIDPass to text returned of (display dialog "Enter the
passphrase for " & remoteID default answer "")
		else
			tell me to quit
		end if
	end try
end try

-- assemble new user details
set {nextID, defaultHome, availShells, inuseUsernames} to my
remoteInfo(remoteIDPass, remoteID, remoteMac)
set realName to quoted form of (text returned of (display dialog "Enter the
new user's full name" default answer "Remote New User"))
set NewUsershortname to my getShortName(realName, inuseUsernames, "")
set aShell to choose from list availShells with prompt "pick a shell for " &
realName default items "tcsh"
set uid to text returned of (display dialog "Pick a uid for " & realName & ".
Don't change unless you know what you're doing." default answer nextID)
set gid to text returned of (display dialog "Pick a gid for " & realName & ".
Don't change unless you know what you're doing." default answer 20)

set home to text returned of (display dialog "Enter path to " & realName &
"'s default home folder (netinfo)" default answer defaultHome &
NewUsershortname)

-- make this smarter.
-- Maybe allow setting this to only appear when creating a user on a specific
(by IP) machine that is known to use a different location for user dirs
set actualHome to text returned of (display dialog "Enter path to " &
realName & "'s actual home folder (mkdir)" default answer "/Users/" &
NewUsershortname)

-- offer final chance to back out
set finalChance to button returned of (display dialog "You are about to
create a new user on " & remoteMac & " with the following properties:" &
return & return & ¬
	"real name: " & realName & return & ¬
	"short name: " & NewUsershortname & return & ¬
	"shell: " & aShell & return & ¬
	"uid: " & uid & return & ¬
	"gid: " & gid & return & ¬
	"default (netinfo) home directory: " & home & return & ¬
	"actual (mkdir) home directory: " & actualHome buttons {"Cancel",
"Create"} default button "Create" with icon 1)
if finalChance = 1 then
	set remoteIDPass to missing value
	return -128
end if

-- create new user
try

	my barberPole("Creating user. ", 5)

	set newuserpass to "123"
	do shell script "echo " & remoteIDPass & " | ssh -l " & remoteID & " " &
remoteMac & " sudo -S niutil -create \\/ \\/users/" & NewUsershortname & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S niutil -createprop \\/ \\/users/" & NewUsershortname & " shell
/bin/" & aShell & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S niutil -createprop \\/ \\/users/" & NewUsershortname & " realname
\"" & realName & "\"" & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S niutil -createprop \\/ \\/users/" & NewUsershortname & " uid " &
uid & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S niutil -createprop \\/ \\/users/" & NewUsershortname & " gid " &
gid & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S niutil -createprop \\/ \\/users/" & NewUsershortname & " home " &
home & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S niutil -createprop \\/ \\/users/" & NewUsershortname & "
_shadow_passwd " & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S niutil -createprop \\/ \\/users/" & NewUsershortname & " sharedDir
" & "Public" & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S mkdir \\/" & home & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S cp -R /System/Library/UserTemplate/English.lproj/ " & actualHome & ¬
		"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac &
" sudo -S chown -R " & NewUsershortname & ":staff " & actualHome

	-- to also set passwd, something like this, kind of...:
	--"; echo " & remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac & "
echo " & newuserpass & " passwd " & NewUsershortname

	tell me
		set remoteIDPass to missing value
		activate
		display dialog "User created. Use the shell and type \"passwd " &
NewUsershortname & "\"to create the user's passphrase." buttons {"OK"}
default button 1 with icon 1
	end tell
on error m number n
	set remoteIDPass to missing value
	tell me
		beep
		activate
		display dialog (n as text) & return & return & (m as text)
	end tell
end try


-- get current user's short name
on getMyshortname()
	set Myshortname to ""
	try
		set Myshortname to (do shell script "whoami")
	end try
	return Myshortname
end getMyshortname

on remoteInfo(remoteIDPass, remoteID, remoteMac)

	my barberPole("Gathering info from remote Mac. ", 5)

	-- get remote uids
	set nextUID to (last word of (do shell script "echo " & remoteIDPass & " |
ssh -l " & remoteID & " " & remoteMac & " nireport / /users name uid | grep
\"5[0-9][0-9]\"") as number) + 1

	-- get remote usernames
	set existingUserNames to every word of (do shell script "echo " &
remoteIDPass & " | ssh -l " & remoteID & " " & remoteMac & " nireport /
/users name uid")
	set inuseUsernames to {}
	repeat with i in existingUserNames
		try
			set i to i as number
		on error
			copy i to end of inuseUsernames
		end try
	end repeat

	-- get remote default home path
	set defaultHomeDirectory to (do shell script "echo " & remoteIDPass & " |
ssh -l " & remoteID & " " & remoteMac & " echo $HOME")

	set charCount to (count of characters of remoteID) + 1
	set defaultHomeDirectory to (characters 1 thru -charCount of
defaultHomeDirectory) as string

	-- get remote available shells
	set availShells to {}
	repeat with i in shellList
		try
			set shellExists to (do shell script "echo " & remoteIDPass & " | ssh
-l " & remoteID & " " & remoteMac & " which " & i)
			copy i to end of availShells
		on error
			-- this shell not available
		end try
	end repeat

	return {nextUID, defaultHomeDirectory, availShells, inuseUsernames}
end remoteInfo

on barberPole(aString, theDuration)
	display dialog aString & "This may take a minute." buttons {"OK"} default
button 1 with icon 1 giving up after theDuration
end barberPole

on getShortName(realName, inuseUsernames, aString)
	set NewUsershortname to text returned of (display dialog aString & "Enter
a short username for " & realName & ". Do not use spaces!!!" default answer
"rmnu")
	repeat with i in inuseUsernames
		if (i as string) = (NewUsershortname as string) then
			beep
			-- try again with:
			set NewUsershortname to my getShortName(realName, inuseUsernames,
"This username is not available. " & return & return)
		end if
	end repeat
	return NewUsershortname
end getShortName


--
Sander Tekelenburg, <http://www.euronet.nl/~tekelenb/>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/mailman//archives/applescript-users

This email sent to email@hidden

References: 
 >add users remotely (From: Rishi <email@hidden>)

  • Prev by Date: Where does Mail store deleted messages?
  • Next by Date: Re: cleaning ^Z out of files
  • Previous by thread: Re: add users remotely
  • Next by thread: Re: Searching for RGB images in a Quark doc
  • Index(es):
    • Date
    • Thread