Apple Remote Desktop - Executing Scripts Using Send UNIX Command

background image

Executing Scripts Using Send UNIX Command

There are two kinds of scripts you can execute via the command line. First, and most
common with command lines, is a shell script. A shell script is a file containing a
collection of UNIX commands that are all executed in sequence. Shell scripts can have
normal programming procedures like loops, conditionals, and variables. Shell scripts
are text files with UNIX line endings. Shell scripts are interpreted using the bash shell.

The second kind of script you can execute, and the most common in the Mac OS X
environment, is an AppleScript. AppleScripts are files that contain English-like
commands, using the AppleScript programming language and they are created using
the Script Editor application.

Running a UNIX command as the current user will fail if the target computer is at the
login window, since there is no current user at that point. You can use root user for
tasks by entering root in the specified user field of the task dialog. You don’t actually
need to have the root account enabled on the client computer to specify the root user.
You should never use

sudo

or

su

to do tasks as the root user. They are interactive and

expect further input and response from your script. Instead, run your script as root or
whatever user you were planning on.

Executing Shell Scripts with Remote Desktop
Shell scripts can be copied, then executed. If a script has any degree of complexity, or if
it cannot be expressed on a single line, you can use Copy Items to copy the script file to
the client computers, then execute it using Send UNIX Command. To send a single-line
command you can simply use Send UNIX Command.

background image

158

Chapter 8

Administering Client Computers

To copy and execute a script:

1

Prepare and save your script.

Make sure your script is saved as plain text with UNIX line breaks.

2

Open Remote Desktop.

3

Select a computer list in the Remote Desktop window.

4

Select one or more computers in the selected computer list.

5

Use the Copy Items command to copy your script to the client computers.

See “Copy Options” on page 116 and “Copying from Administrator to Clients” on
page 118 for more information.

6

After copying the script, choose Manage > Send UNIX Command.

7

Execute the script by typing:

sh script pathname

8

Click Send.

Executing AppleScripts with Remote Desktop
AppleScripts can be executed on client computers in two ways. They can be saved and
executed as an application, or sent at once using the command line.

To learn more about AppleScript, see AppleScript Help in Help Viewer or go to:
www.apple.com/applescript/.

To send and execute an AppleScript:

1

Save the AppleScript as an application.

2

Open Remote Desktop.

3

Select a computer list in the Remote Desktop window.

4

Select one or more computers in the selected computer list.

5

Use the Copy Items command with the Open Items option selected in the Copy Items
dialog.

See “Copy Options” on page 116 for more information.

To execute an AppleScript using the Send UNIX Command:
This method uses the

osascript

command. See the

osascript

man page for more

information.

1

Select a computer list in the Remote Desktop window.

2

Select one or more computers in the selected computer list.

3

Choose Manage > Send UNIX Command.

4

Type or paste the AppleScript in the UNIX Command window, like this:

osascript -e 'First line of script' -e 'Next line of script' [ -e ... ]

background image

Chapter 8

Administering Client Computers

159

Alternatively, you could use a UNIX “read standard input” redirection which looks like:

osascript <<EndOfMyScript

...insert script here...

EndOfMyScript

For example, a simple script to create a folder and set its label would be entered as:

osascript <<EndOfMyScript

tell the application "Finder"

make new folder

set the name of the result to "New Folder"

set the label index of folder "New Folder" to 2

end tell

EndOfMyScript

5

Click Send.

The client computer executes the script.