Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 21, 2006, 02:29 PM   #1
twynne
macrumors 6502a
 
Join Date: Apr 2006
Location: London, UK
Applescript vs. Terminal - Help needed

Hi all,

I'm new to Mac/OSX and I'm looking for a bit of guidance. In brief I have a program (Poweroff) which runs as a service on several Windows machines on my network which I can use to remotely standby/poweroff/run programs etc. Previously I used the same program on my Windows laptop to send commands to the servers, but the program doesn't exist (yet) for the Mac. Fortunately the server side exposes a telnet interface, so I thought I could just create an Applescript for each machine to connect via telnet and issue commands.

I'm basically looking for the following:

Launch a terminal session
Telnet to {server} {port}
Send command 'Password {password}'
Send command 'Action Standby'
Send command 'Wait 10'
Send command 'Doit'
Send command 'Quit'
Close the telnet session
Quit terminal

It would be even better if the script could prompt me for the 'Action' (standby, shutdown, etc.) and substitute this in the script.

So far I've managed to launch terminal and issue the telnet command. Unfortunately I can't figure how to issue multiple commands each as a separate entry.

I'm sure this is pretty basic stuff for most of you, but any advice much appreciated.

Thanks in advance,

Tom
twynne is offline   0 Reply With Quote
Old Apr 21, 2006, 03:00 PM   #2
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
after testing out the commands in terminal you can use
Code:
do shell script ("<insert terminal command here>")
in applescript to send commands to the command line (as long as each one isn't dependant on the results from the last...)
__________________
Actually it does make sense. Man created god, so if we exist, He exists. - obeygiant
Eraserhead is offline   0 Reply With Quote
Old Apr 21, 2006, 03:17 PM   #3
twynne
Thread Starter
macrumors 6502a
 
Join Date: Apr 2006
Location: London, UK
Thanks for the response. When I try this:
tell application "Terminal"
activate
do shell script "telnet server port; password password; action standby; seconds 10; doit; quit"
end tell
I get an Applescript error:

Terminal got an error: Connection closed by foreign host.
sh: line 1: password: command not found
sh: line 1: action: command not found
sh: line 1: seconds: command not found
sh: line 1: doit: command not found
sh: line 1: quit: command not found
What am I doing wrong?

Thanks again,

Tom
twynne is offline   0 Reply With Quote
Old Apr 21, 2006, 03:28 PM   #4
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by twynne
Thanks for the response. When I try this:
tell application "Terminal"
activate
do shell script "telnet server port; password password; action standby; seconds 10; doit; quit"
end tell
I get an Applescript error:

Terminal got an error: Connection closed by foreign host.
sh: line 1: password: command not found
sh: line 1: action: command not found
sh: line 1: seconds: command not found
sh: line 1: doit: command not found
sh: line 1: quit: command not found
What am I doing wrong?

Thanks again,

Tom
Sorry I wasn't very clear, if you type this literally into applescript (no tell app terminal or anything..) it'll make a new folder on your desktop (called hello)
Code:
do shell script "mkdir ~/Desktop/Hello"
see http://developer.apple.com/technotes/tn2002/tn2065.html for some more information
if you do this in the terminal application it'll do the same.
__________________
Actually it does make sense. Man created god, so if we exist, He exists. - obeygiant
Eraserhead is offline   0 Reply With Quote
Old Apr 21, 2006, 04:07 PM   #5
twynne
Thread Starter
macrumors 6502a
 
Join Date: Apr 2006
Location: London, UK
That link is somewhat helpful in that I think it's identified the issue:
Q: How do I control an interactive tool like ftp or telnet with do shell script?
A: The short answer is that you donít. do shell script is designed to start the command and then let it run with no interaction until it completes, much like the backquote operator in Perl and most Unix shells.

However, there are ways around this. You can script Terminal and send a series of commands to the same window (though this only works in Mac OS X 10.2 and later), or you could use a Unix package designed for scripting interactive tools, such as expect. Also, many interactive commands have non-interactive equivalents. For example, curl can substitute for ftp in most cases.
So it sounds like I should "script Terminal" which I assume takes me back to the original attempt I was making above. The only question is how do I send a series of commands to the same Terminal session?

Many thanks again - I'm learning!
twynne is offline   0 Reply With Quote
Old Apr 21, 2006, 04:55 PM   #6
twynne
Thread Starter
macrumors 6502a
 
Join Date: Apr 2006
Location: London, UK
Think I got it!! The following worked:

tell application "Terminal"
activate
do script "telnet server 9999" in front window
do script "password password" in front window
do script "action standby" in front window
do script "seconds 5" in front window
do script "doit" in front window
do script "quit" in front window
say "server instructed to stand by"
quit
end tell


Again many thanks for your help and patience!
twynne is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Help needed for simple Applescript duckdealer Mac Programming 2 Apr 7, 2014 10:54 PM
How to create an applescript for multiple dd terminal commands in sequence ? AlbertoBC Mac Programming 2 Feb 2, 2014 01:30 PM
Applescript to make terminal close automatically? Ri-dog Mac Programming 1 Jan 18, 2013 12:53 PM
applescript leaves terminal window open Zeke D Mac Programming 6 Jul 22, 2012 02:44 PM

Forum Jump

All times are GMT -5. The time now is 06:57 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC