"script" command followed by multiple commands not working

I am a bit of a noob on applescript and couldn’t find anything in my searches for what I am doing wrong. Hopefully it is an easy fix

I am trying to write an applescript that will start the "script"command so I can record all the results of the different commands I am running in a text file so I can review it later.

Below is an example of what I am trying to do. When I run the applescript, the script command executes but then nothing else runs. After the script command runs, the command prompt changes and reads “bash-2.3$” I am pretty sure this is what is stopping the rest of the command from running. Is there a way around this?

tell application "Terminal"
	do script "script /Volumes/Data_Disk/Command_Results.txt"
	do script "date" in window 1
	do script "history" in window 1
	do script "mount" in window 1
	do script "netstat" in window 1
	do script "exit" in window 1
end tell

Thanks for the help
Dave

Model: MacBook Pro 15
AppleScript: 2.2.3
Browser: Safari 537.22
Operating System: Mac OS X (10.8)

Hello and welcome to Macscripter! :slight_smile:

If I read you correctly, you are looking for some kind of a keylogger facility, that can track what you have done in the Terminal window. (Should get iterm2 really).

You have the history facility of bash, but if you want to track the output of the commands, then man script may be something for you.

I would not try to use this command from a do script command, since it works so closely to the tty. You can “emulate it” by setting the size of the terminal history, (roll back or whatever in the second tab of terminal preferences, down to the right), to unlimited.

Then you can grab the contents of the terminal window and save that to a file when you are done.
This one grabs whatever you have typed since you started the Terminal session, based on your history setting as I mentioned higher up, and sends the result to the clipboard.

tell application "Terminal"
	tell front window
		set the clipboard to history of selected tab as text
	end tell
end tell

HTH

Terminal needs to have a window open to run the script in. As written your script doesn’t open one, adding “activate” cures this.


tell application "Terminal"
	activate
	do script "script /Volumes/Data_Disk/Command_Results.txt"
	do script "date" in window 1
	do script "history" in window 1
	do script "mount" in window 1
	do script "netstat" in window 1
	do script "exit" in window 1
end tell

Hello.

I suggest this.


tell application "Terminal"
	activate
	do script "date" in window 1
	do script "history" in window 1
	do script "mount" in window 1
	do script "netstat" in window 1
	tell window 1
		set the clipboard to history of selected tab as text
	end tell
	do script "exit" in window 1
end tell

writeClipboard for "/Volumes/Data_Disk/Command_Results.txt"
to writeClipboard for aPxPath
	set a to the clipboard as text
	set theFile to POSIX file aPxPath
	set fref to open for access theFile with write permission
	write a to fref as text starting at 0
	close access fref
end writeClipboard

Edit

The use of Unix tools to write the output of the nestat command turned out to not work so well???

I don’t know why it didn’t work, but the new handler works! :slight_smile:

Thanks McUserII and Av8TnTek for all the suggestions. Using activate seems to work, the rest of the commands are executing and the text file is being populated. Will continue to work on it and test.

I am using this for a computer forensics class to pull and record the output of each command.

Again, thanks for the help!!!
Dave

Av8TnTek

Hello.

I got finding a way of getting terminal output that may contain “bad byte sequences” into a file. So I can get netstat output and such (top).

Not that I need that often. :slight_smile:

If I were you, and this is a submission, I’d read up on the parameters to the script command. Start it with proper parameters, AND writing a script file to a temp folder, setting it executable, and call that script to end the terminal session, after having quitted script. Or, create an alias for that exit script.

I don’t think it is necessary, but a nice touch. :slight_smile: Actually I’ll implement something like that on my own, for iTerm, which is much better, once you have configured the keyboard. (Slightly different from terminal, but better in the end.)

I have tried to get this working correctly and just can’t seem to get it. If I run the script, nothing happens. Sometimes if I run it twice, it will work and provide the output I want. Can anyone give me an idea what I am doing wrong?

Basically, this is a script that uses known commands located on a USB drive to grab volatile data from a system and send its output to the drive.

Thanks in advance.

 tell application "Terminal"
	activate
	do script "PATH=/Volumes/Target/usr/bin:/Volumes/Target/bin:/Volumes/Target/usr/sbin:/Volumes/Target/sbin:/Volumes/Target/usr/X11/bin" in window 1
	do script "date" in window 1
	do script "history" in window 1
	do script "mount" in window 1
	do script "netstat" in window 1
	do script "ps -ax" in window 1
	do script "ifconfig" in window 1
	do script "id" in window 1
	do script "lsof" in window 1
	do script "who" in window 1
	do script "jobs" in window 1
	do script "lsof" in window 1
	do script "date" in window 1
	tell window 1
		set the clipboard to history of selected tab as text
	end tell
	do script "exit" in window 1
end tell

writeClipboard for "/Volumes/Target/Reports/Command_Results.txt"
to writeClipboard for aPxPath
	set a to the clipboard as text
	set theFile to POSIX file aPxPath
	set fref to open for access theFile with write permission
	write a to fref as text starting at 0
	close access fref
end writeClipboard

Hello.

I hope your realize, that lsof for instance takes a lot of time, so you should put in an appropriate delay after it, to not try to execute the next steps prematurely.

The next issue is of course whether you have the rights to execute the commands in the path, that is that you are having an id, and a group id, that lets you execute them.