Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twynne

macrumors 6502a
Original poster
Apr 21, 2006
805
45
London, UK
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
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
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...)
 

twynne

macrumors 6502a
Original poster
Apr 21, 2006
805
45
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
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
twynne said:
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.
 

twynne

macrumors 6502a
Original poster
Apr 21, 2006
805
45
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

macrumors 6502a
Original poster
Apr 21, 2006
805
45
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.