Capture terminal window response.

Hi All,

Use the terminal to login to a remote unix FTP server and query the available disk space using the “df -h” command.

Is there a way to capture the returned to a text file using AS?

I can then automate the process to send an alert based on the number returned.

Query the server.
Write the result to a text file
Use AS to query the available space no. in the text file.
Generate an alert if required.

Currently the script looks like :

tell application "Terminal"
	do script "ssh -l root" <- Logs into remote server
	delay 4
	do script "xxxx" in window 1 <- enters a required password
	delay 2
	do script "df -h" in window 1 <- queries disc space
	delay 2
end tell

Thanks in Advance.

You can fuse scripts together:

do shell script "xxxx && echo bbbb && command -t -b 


Hi Dylan,

No joy this end.

The separate “do scripts” statements were to allow for a delay in entering the commands into the terminal.

Trying your suggestion just concatenated the commands into a jumble. Perhaps you could post some sample code that demonstrates this that I could run to confirm syntax is correct.

What are the “-t” & “-b” switches meant to achieve? Will this help dump the text to a file? Please explain.

Warm Regards,


You might find what you need inside the history or contents properties of the Terminal window. Relying on history means that you need make sure that Terminal’s history is enabled and configured with enough lines to capture all of your output. Relying on contents means that you have to make sure that the output will fit in the visible part of the window. Because of these configuration requirements, neither of those is very appealing to me, but they might be satisfactory for your purposes.

An alternative is to use the script command to capture the tty output. Because of buffering, you will need to exit the shell before you can guarantee the data has been stored in the specified file. If you were planning on running other commands after the df that depended on the output of df, then this may not be suitable.

set tmp to do shell script "mktemp -t output_capture"
tell application "Terminal"
	do script "exec script " & quoted form of tmp
	delay 2
	do script "exec ssh -l root" in window 1
	delay 4
	do script "Th3Pa5sW0rD" in window 1
	delay 2
	do script "df -h; exit" in window 1
	delay 2
end tell
set allOutputIncludingDF to do shell script "set -- " & quoted form of tmp & "; cat \"$1\"; rm -f \"$1\" >/dev/null 2>&1"

This still has the element of “blind typing” that I really dislike. The program is effectively typing into a Terminal window but never looking at what comes back (until the end). What if the ssh fails? If Terminal is configured to close its window when the shell exits and some other Terminal window is already open, then the rest of the “typing” is sent to some other window (since the one created by this script will close, making window 1 refer to some other window). What if the user switches windows while the script is running? There are many potential complications here.

An all around better way would be use use key-based authentication to avoid the password prompt (maybe with SSHKeychain since the ssh in 10.4 does not automatically integrate with the user’s Keychain(s)) and tell ssh to directly run the df command. Since these changes would completely automate the command, it can be run with do shell script so that you can avoid the vagaries of having to deal with Terminal.

Using SSHKeychain (which you could skip if you use an unencrypted ssh key (this is not recommend though!)) and a specified key (identity) file (only required if you do not add want to add your default key as an authorized key for your destination account) it might look like this:

set kcPrefix to "SSH_AUTH_SOCK=\"/tmp/$(id -u)/SSHKeychain.socket\" "
set identOpt to " -i ~/.ssh/special_id "
set dfOutput to do shell script kcPrefix & " ssh " & identOpt & " -l root remoteHostname df -h"