Control Your Mac from Afar
by Harold Martin09/19/2003
There are many different ways to control your Mac -- even when you're not sitting at it. You might think that this level of flexibility would require special software. But no! If you're running Mac OS X, you'll be able to accomplish everything in this article without buying a single piece of software. As a bonus, you can also perform most of these tricks on the Mac you're sitting at right now. (Even though that wouldn't actually be remote controlling, now would it?)
The Tools We'll Use
ssh
ssh is the Secure SHell. It and its accompanying programs allow you
to securely log in and copy files to other computers running an ssh server.
The target Mac must have OS X's built-in ssh server enabled. You can
do this by checking "Remote Login" in the "Services" tab in the "Sharing"
System Preference pane. ssh is the "remote" part in "remote control:" we use ssh to log in to
the Mac that we want to control, and then use the other technologies
we'll talk about below to do the actual controlling. The computer you'll
use to control the target Mac should have an ssh client. Most types of UNIX
(including OS X) come with an ssh client, and clients are available
on just about every other platform, including Windows and OS 9. If you're
interested in learning more about ssh, I recommend SSH,
The Secure Shell: The Definitive Guide.
Built-In Commands
On top of the normal UNIX commands we'll use, Mac OS X has a couple of extra ones that will be particularly helpful to us:openis a command that can open a file, directory, application, or URL from the command line, just as if it was double-clicked in the Finder.screencaptureis a program that (surprise, surprise) takes a screen shot of what is on the computer's screen.
AppleScript
|
Session by Gordon Meyer:
Upgrade your digital life to include your house and living environment--controlling lights, temperature, music, and more--all from your Mac. We'll start with the basics of controlling lighting and other physical objects, then graduate to true automation which turns your home into a living entity that responds to, and anticipates, your needs. O'Reilly
Mac OS X Conference |
AppleScript, as you probably know, can be used to control almost everything
on your Mac (now even more so, with GUI
Scripting). You can run AppleScripts from the command line with
osascript scriptname. You can also write scripts
on the fly with osascript -e 'line 1' -e 'line 2'. This
is an amazing tool that we'll use a lot.
cron
We'll use cron to run AppleScripts and commands automatically at set
times.
Remember ...
In order for open, screencapture, or osascript to work over ssh, you
must have either ssh'd in as the user currently running, or you must
run them with sudo. In other words, if user Alice is logged
in at the actual machine and you ssh in as Bob, you would need to run
sudo open file if you wanted file to appear
on Alice's screen. On the other hand, if you logged in as Alice, you
would only have to run open file. Keep this in mind; it's
tripped me up before.
Let's Do It!
Now that you've got your tools in order, I'll walk you through a few tricks to get you started with remote-controlling magic. Well, it's not really magic -- just some good old-fashioned UNIX and Apple technologies.
Logging In
In OS X, open up Terminal and type:
ssh username@target's IP
Replacing the target's IP and the username you use on the target, as appropriate.
If you're using a non-UNIX ssh client, check its documentation for instructions
on logging in.
After a successful login, you'll get a command prompt from which you'll execute the tricks below!
Seeing What the Target Is Doing
To start, go to the prompt and enter:top
You'll see what processes are running, sorted by CPU usage. The processes
whose names end in .app are OS X applications. If you want
to see just the applications, enter:
ps -aux | grep '.app'
ps will list all processes, and grep will display
all of the lines that contain ".app".
Now, if you want to see exactly what the other computer is up to, you could enter:
screencapture -x ~/screen.pdf
This will take a screenshot (without the "camera click" sound) that will
be saved to the file screen.pdf. But how do you view it? By using scp
to copy the screenshot to your computer! If you're still in your ssh
session, you should type:
scp ~/screen.pdf yourusername@yourIP:
substituting your username and IP. Be sure to have the ":" at the end,
otherwise scp won't recognize that you want to copy to
another computer.
An alternate way to get the screenshot is to exit your ssh session and,
at your local prompt, type:
scp otherusername@target's IP:screen.pdf screen.pdf
Again, substitute the correct IP and username.
Either way, you'll end up with the screenshot file screen.pdf in your home directory, which you can look at and see exactly what the other computer was doing!
Communicating With the User of the Other Computer
Now, what if you want to say something to the person using the other
computer? To do this, we'll use the dynamic duo of ssh and osascript.
Logged in over ssh, you can enter:
sudo osascript -e 'tell app "Finder" to activate' -e 'tell app "Finder"
to display dialog "I see you!"'
All that this does is run a two-line (two -es) script that activates
the Finder (which is necessary in order for the dialog to have some
place to appear) and then displays the dialog. You can use any AppleScript
you want, and you can also get the results of what the user on the target
Mac did. For example you could use something like this to let the user
talk back to you:
sudo osascript -e 'tell app "Finder" to activate' -e 'tell app "Finder"
to display dialog "What would you like to say?" default answer ""'
|
|
In the text printed out over the ssh session, you will see a line like
"text returned:Hi!, button returned:OK". The text returned will be what
the user typed back.
But what if you actually wanted to talk to the user, with a voice? You would enter this:
sudo osascript -e 'say "Now I can watch you and talk to you"'
A couple of notes, though: I've sometimes had problems using speech with sudo,
so you might also want to try this without sudo. Also keep in mind that this
will have no effect if the target Mac is muted. You might want to use say
and display dialog for maximum effect!
Closing and Opening Applications
Think back to the discussion of seeing what the target Mac is doing.
What if you see an application that you want to close? From the listing you
get from ps -aux, you can get an application's process
ID (PID). You can then take that PID and pass it to the kill command:
kill PID
I've found this to be particularly useful when an application won't quit
(even with force quit) or when it's in fullscreen mode (like a game).
I can just log in from another machine and quit the wayward app, then
get back to work. If something freezes so bad that normal kill won't,
well, kill it (a rarity) then you can use "super" kill:
kill -9 PID
The only problem with killing an app is that it will immediately
close down, giving the user no chance to save any open documents first.
What if you would like to close down the application in a kinder manner?
AppleScript to the rescue! You can quit TextEdit and still allow the
user to save his changes by entering:
osascript -e 'tell app "TextEdit" to quit'
Be careful, though; if TextEdit isn't already open, then this script will
cause it to open and then quit. Use ps to make sure TextEdit
(or whatever application) is open in the first place. Also keep in mind
that the user can simply click the Cancel button to keep the app from
quitting.
Shutting Down or Rebooting
Suppose you want to shut down or reboot the computer. As before, there's the immediate UNIX way and the kinder (but user-cancelable) AppleScript way. The command-line way to shut down is:
sudo shutdown -h now
This will close your ssh session and shut down the computer, regardless
of any apps or documents that may be open. To reboot, simply enter:
sudo reboot
Again, this will close everything down without mercy. To give the user a chance to save what they're doing (and yes, to cancel the shutdown) you can type:
osascript -e 'tell app "Finder" to shut down'
or
osascript -e 'tell app "Finder" to reboot'
to shut down and reboot, respectively. The only way a user can cancel this is to keep an application open long enough that the shutdown or reboot times out. But at least it won't give the user the "Are you sure you want to shut down your computer now?" dialog!
Using AppleScript GUI Scripting to Control any Application
Apple has released a beta scripting engine, called GUI Scripting, that allows AppleScripts to take full control of any application by directly interacting with its GUI elements (such as menus and buttons). First, download it from Apple's GUI Scripting page. There are also some example GUI scripts there that you should look over. Here is a simple script that puts the computer to sleep, something that can't be accomplished via the Finder's AppleScript dictionary or with regular UNIX commands:
try
tell application "Finder"
activate
end tell
tell application "System Events"
with timeout of 30 seconds
tell process "Finder"
tell menu bar 1
click menu item "Sleep" of menu "Apple"
end tell
end tell
end timeout
end tell
end try
You should save this script it as sleep.scpt, preferably in a hidden directory (such as ~/.applescripts). The next time you log in, you can just enter:
osascript .applescript/sleep.scpt
though putting the computer to sleep will freeze your ssh session.
GUI Scripting will probably end up being one of the most useful tools in your toolbox, as what you can accomplish with it is (nearly) infinite, at least as far as controlling your Mac goes.
Automating Your Remote Control
Now that we've covered how to use a variety of UNIX commands and AppleScripts to control your Mac, there's only one major obstacle left to complete control: you have to be at a computer to do the controlling. Wouldn't it be nice if you could tell your Mac to take control of certain things at certain times?
Enter cron, the age-old UNIX command-scheduling tool. If
you don't know how to use cron, you should read this
tutorial or Chris Stone's "Learning
the Terminal in Jaguar." Don't worry, cron is actually
quite simple.
Since you can't be sure which user (in any) will be logged on when the
cron job runs, you have to edit root's crontab.
With that in mind, you can grab your favorite UNIX text editor, run it
as sudo, and edit /etc/crontab; or, you can use a GUI tool like CronniX.
The only difference between CronniX and /etc/crontab is that in CronniX
you don't use the who field. If you choose to use CronniX,
you open root's crontab by clicking Open and typing in root
for the username.
* * * * * root sudo osascript -e
'tell app "Finder" to activate' -e 'tell app "Finder" to display
dialog current date as string'
Though of course, you would never put that in your crontab, since it would drive you as well as all of your users crazy
Full Remote GUI Control
Finally, if you want complete control over the GUI, there's VNC. VNC allows you to open a window that shows the exact the desktop of the other computer, so you can move the other user's mouse, type in their dialog boxes, and otherwise annoy them. OSXvnc and Chicken of the VNC are my favorite server and client, respectively. After launching your VNC server, you can use any computer that has VNC client to log in and do your remote controlling. The biggest downside to VNC (and the reason it is not covered more extensively here) is that it is totally interactive and is difficult (if not impossible) to automate.
|
|
exit
You're well on your way to automating and controlling your Mac. Using both old and new tools, you should be able to use your Mac whether you're sitting in front of it, on another computer, or letting it execute timed tasks itself. Read sites like Mac OS X Hints and O'Reilly Hacks (and their books Mac OS X Hints: Jaguar Edition and Mac OS X Hacks) to find more tricks you can use.
With all the possibilities, I'll be excited to see what you come up with. Post your experiences and ideas in the Talkback section below!
Harold Martin is a freelance software developer and author. Visit him at his blog.
Return to the Mac DevCenter
Showing messages 1 through 22 of 22.
-
Dialog Box
2006-10-15 07:08:00 jedi22 [Reply | View]
When ever I type "sudo osacript......." it never works. It says it expect end or end tell but found to. How can I get this to work?
-
Password Problems
2004-11-20 09:08:49 z_coil [Reply | View]
Whenever I try to log on through ssh it asks for the password but then rejects it saying "Permission denied, please try again." 2 times and then says "Permission denied (gssapi,publickey,password,keyboard-interactive)." What am I doing wrong?
-
Instead of using applscript to talk...
2004-10-19 12:27:33 maymay [Reply | View]
You can just use the Mac's built-in say command.
say "Listen to me talk to you."
instead of
sudo osascript -e 'say "Listen to me talk to you."'
Both commands have the same effect.
-
You can also use Eggplant
2003-10-30 14:32:59 anonymous2 [Reply | View]
It is a more expensive solution, but it also works much better.
http://www.redstonesoftware.com
Redstone is also the maintainer of OSXvnc.
Then you have HyperTalk-like scripting built into Eggplant.
Jim
-
Router ... vulnerable?
2003-10-28 13:27:20 anonymous2 [Reply | View]
Hi, my friend today said that our school's router is has port 22 open for SSH. I can barely remember what she said. Something about a Denial of Service attack (don't even know what that is) would make it shut down. Could this happen or is she all talk?
-
the basics
2003-10-28 13:24:31 anonymous2 [Reply | View]
I tried this today (connecting to myself) with the [sudo osascript -e 'tell app...] command. I'm very new to applescripting and mac's in general... Do the -e's actually represent carriage returns? And are you supposed to write out "application" and not use "app". Needless to say, I had trouble. I connected to myself, but the commands wouldn't work. I'll try experimenting more later. Thanks for any help.
-
deja vu!
2003-10-27 05:44:00 d_vaught [Reply | View]
MMM...seen that repeating VNC picture somewhere before (love the caption).
Anyone have a clue what I'm doing wrong?
-
SSH into LAN'd Mac?
2003-10-06 00:11:11 anonymous2 [Reply | View]
I've got my Macs on a router sharing an IP. How do you log in to one of these? Would it just be logging in to the IP given to the router by the DHCP, but since you are going into a user, it knows where to go? I think not? Could do manual IPs on the systems but those are private, so how would one go to the main address, then choose which local private addy to go into? I'm thinking doing a DYNDNS setup that updates upon IP change, but that's still only going as deep as the router. Would you have to port forward? And if so, can that only be to one machine, not multiples?
thanks for any info,
No clue
-
Does it to Aqua forwarding?
2003-10-02 10:39:10 anonymous2 [Reply | View]
What would be REALLY cool is if Apple supported aqua forwarding--similiar to X-forwarding over SSH. Here's what I envision: log in from a Mac(client) via SSH to a Mac(server), and then lauch an app and have the windows for that app only show up on the client machine. Currently you can launch an app from the client machine, but it only shows up on the server, not the client. Is there any way to do this, besides VNC, which does the whole screen?
-
Wake on LAN
2003-10-01 01:20:04 anonymous2 [Reply | View]
I had a similar problem with a mac, whereby it would wake on lan when I had put it to sleep with the sleep menu item in the apple menu, but if it went into a sleep itself (from idle time) then it would not wake on lan.
I sent the wake on lan packet to the ethernet address as others describe here.
Not sure why this was. I just leave it on all the time now and I don't have to worry.
-
http://www.codon4.com
2003-09-27 08:09:05 anonymous2 [Reply | View]
well, if you've ever heard of http://www.codon4.com then you knew about this A LONG time ago...
-
can't get screenshot
2003-09-24 11:56:53 network23 [Reply | View]
This is the second article I've seen on how to get the screenshot of a remote machine and the second time I've tried it and failed. I get this error message...
kCGErrorIllegalArgument : initCGDisplayState: cannot map display interlocks.
kCGErrorIllegalArgument : CGSNewConnection cannot get connection port
screencapture: failed to make a connection to the windowserver
Any ideas? Firewall issue? I don't know.
-
Talk
2003-09-22 10:27:20 bizard [Reply | View]
Although the chat method you cited was novel, it would be rather difficult to carry on a decent conversation using the applescripts and dialogs. You could, however, use the dialog box to say 'Open the terminal and talk to me.'
Of course, I just tried this to make sure it worked before posting and it seems that I have a problem with my terminal settings and it doesn't work. Anyway, if someone feels like explaining how to get 'talk' (an old unix chat program) it is a nice way to communicate with another logged in command line user.
-
ps
2003-09-21 18:48:29 anonymous2 [Reply | View]
If you really want to find all the processes that invoke applications (well, those that end in ".app") you'll need to use the -ww flags to ps. Otherwise long application paths aren't going to show under a simple ps -aux invocation. That is, the command in the article should be something like
% ps -wwaux | grep ".app"
Cheers,
Paul
-
Wake on LAN
2003-09-21 10:32:15 anonymous2 [Reply | View]
Great article. I was expecting to see something about waking up a sleeping computer, though. This is something I've been struggling with on my home network: how to wake my sleeping iMac (17" 1gHz) from my powerbook (G4 667), through a router (Netgear MR814). I've worked through the suggestions on macosxhints.com including python and perl scripts. I've also tried utilities like Wake550. Nothing seems to work! My iMac just doesn't seem to pay any attention to those "magic" packets. Any ideas?
Thanks, Mark
-
Excellent Article !!
2003-09-20 13:16:18 GarethMcClure [Reply | View]
I have a small LAN setup at home, comprising of my lovely iMac (OS X) and a windows box running XP.
Using Putty <http://www.chiark.greenend.org.uk/~sgtatham/putty/>, a freeware implementation of Telnet and SSH for Win32 platforms (an SSH client).
I was able to work through this great article and control my mac remotely from the windows box. Thanks for this really great article !!
-
copying screenshots between computers
2003-09-20 04:23:31 anonymous2 [Reply | View]
I've got access to the remote machine and most commands work fine (great instructions - thanks!) but when I try to copy the screenshots between machines I get the following message:
scp ~/screen.pdf myshortusername@myIP:
ssh: connect to address remoteIP port 22: Operation timed out
lost connection
Non-standard rules in my firewall were preventing access initially, but I've now set them to the apple defaults and have allowed ftp on both machines - is there something else I need to do to make it possible to transfer files between the computers? This is just a local network I'm testing on, with the connection running through a router.
-
sleep
2003-09-20 01:39:16 anonymous2 [Reply | View]
Nice article, but I have one minor nit to pick. Actually there is no reason why you cannot remotely sleep the computer by typing osascript -e 'tell application "Finder" to sleep' . No need to use GUI scripting, even though I agree it's pretty cool.
-
ssh ...
2003-09-20 00:02:57 anonymous2 [Reply | View]
how can you put such an article online, not mentioning that, the actual ssh-daemon-implementation (20.09.03) might be vulnerable to a remote exploit ...
wating for an security update by apple ...
see also: http://lists.netsys.com/pipermail/full-disclosure/2003-September/thread.html#10103
-
PPP
2003-09-19 23:42:33 anonymous2 [Reply | View]
How could one manage a modem PPP link using these techniques? I've scripted Internet Connect, but it only seems to work when a user is logged into the remote Mac. Is there a pure Unix solution?
-
Great article, but there's a better downside to VNC
2003-09-19 19:18:51 csoto [Reply | View]
Don't forget that VNC sends ALL data in the clear. This can be bad, as any passwords you enter, or any other sensitive information can be sniffed from the network. See http://www.realvnc.com/faq.html#security for some tips. Of course, since you already know how to use SSH, you should learn how to do tunneling, in order to encrypt ANY traffic, including VNC.
Charles







