Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
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...)
__________________
If they have to tell you every day they are fair you can bet they arent, if they tell you they are balanced then you should know they are not - Don't Hurt me
Eraserhead is online now   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.
__________________
If they have to tell you every day they are fair you can bet they arent, if they tell you they are balanced then you should know they are not - Don't Hurt me
Eraserhead is online now   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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Format vs TM Backup vs Defrag - HELP!!!!!! palebluedot OS X 7 Nov 6, 2008 12:11 PM
Applescript or Terminal - String manipulation Tinlad Mac Programming 2 Dec 14, 2006 09:50 AM
'Fink' messed up my terminal - help please joshuwa72 Mac Applications and Mac App Store 4 Dec 25, 2003 09:45 AM
Apple VS M$ - Cube VS Athens - Developers Need To make a Bluetooth Land-Based Phone walkingmac Macintosh Computers 1 Dec 8, 2003 10:03 AM
Apple VS M$ - Cube VS Athens - Developers Need To make a Bluetooth Land-Based Phone walkingmac General Mac Discussion 2 Dec 8, 2003 09:49 AM


All times are GMT -5. The time now is 09:52 AM.

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

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