Applescript/Telnet Help

Although I'm able to reboot my router using the following applescript:

tell application "Terminal"
activate
do script "telnet 192.168.1.1" in front window
delay 10
do script "password" in front window
delay 2
do script "set reboot" in front window
do script "quit" in front window
say "Router Rebooted"
quit
end tell

..works beautifully, but instead of "delay", can it be like "Wait for password:"?

Also, hope the script is looking good? Please make it shorter/smarter if possible.

Many thanks!

Macbook Pro 17" (v4,1), Mac OS X (10.5.4), 30" Cinema Display

Posted on Aug 11, 2008 1:33 AM

Reply
11 replies

Aug 11, 2008 2:56 PM in response to 3zzy

..works beautifully, but instead of "delay", can it be like "Wait for password:"?


If you don't want to use some shell tool such as expect you can interactively query the state of the window to see what it's saying, e.g.:

...
do script "telnet 192.168.1.1" in front window
repeat until (last word of (contents of front window as text) = "password")
delay 1
end repeat
do script "passwordhere" in front window
...


In this way the script is testing the last word in the window text to determine if it's the prompt you expect. You'll need to play around with the settings to get the right word (e.g. you might need 'word -2' if the prompt says 'password:' since 'password' and ':' would be considered two 'words' as far as AppleScript is concerned).

You should also be aware that using 'front window' is prone to errors should the window focus change mid-stream (e.g. you open a new terminal window while waiting for the router to respond). This may or may not be a big problem depending on your setup.

Aug 11, 2008 3:12 PM in response to 3zzy

You'll have to choose a language other than AppleScript if you want to do things like this reliably.

http://developer.apple.com/technotes/tn2002/tn2065.html#TNTAG5

"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."

Aug 11, 2008 8:22 PM in response to Camelot

Camelot,

That works great, many thanks, but a couple thing:

- Can't we without "front window" so the process is not interrupted by other apps?
- I added a line in repeat to retry connecting until it gets the password prompt:

tell application "Terminal"
activate
do script "telnet 192.168.1.1" in front window
repeat until (last word of (contents of front window as text) = "password")
delay 1
do script "telnet 192.168.1.1" in front window
end repeat
do script "mypassword" in front window
delay 2
say "Router reboot failed. Please check!"
quit
end tell

...but I'm not sure how to set a timeout of say 120 secs?

Thanks again! 🙂

Aug 12, 2008 8:32 AM in response to 3zzy

I added a line in repeat to retry connecting until it gets the password prompt:


I don't see the point in doing this. Either you're going to connect or you're not. If you're not then the problem is a general network problem (e.g. you can't get to the router from where you are), so trying multiple times isn't going to make that any more likely. There's no harm in doing it, I guess, but it doesn't add any extra value to the script.

Can't we without "front window" so the process is not interrupted by other apps?


Yes - what you have to do is keep track of the window the first command is issued in.
What I tend to do is issue the first command without a target window (this opens a new window), then I grab that window's ID and use it as the target for all future commands. e.g.:

tell application "Terminal"
do script "telnet 192.168.1.1" -- this opens a new window
set wID to ID of front window -- grab the new window's ID
repeat until (last word of (contents of window id wID as text) = "password")
delay 1
end repeat
do script "mypassword" in window id wID
...


In this way you can do whatever you like - switch windows, open new ones, etc. and the script will still target the right one (as long as you don't close that, of course).

...but I'm not sure how to set a timeout of say 120 secs?


I assume you mean for the initial connection? Just keep a counter of how many times you've iterated through the repeat loop:

...
set loopCount to 0
repeat until (last word of (contents of window id wID as text) = "password")
set loopCount to loopCount + 1 -- increment the counter
if loopCount = 120 then error -128 -- if we've been here 120 times, cancel the script execution
delay 1
end repeat

Aug 12, 2008 8:51 AM in response to Camelot

1. I've two connections — ADSL and Cable. ADSL is connected via Airport, and Cable via Ethernet and I've separate network locations for both.

So when I'm connecting to Cable, Airport is off. When I switch to ADSL (applescript), it turns on the Airport, which I believe takes about 10-15 sec to make the connection. Therefore I need the script to retry (limited times) untill it can telnet 192.168.1.1 of the ADSL router and notify me if the reboot fails.


wID: Sweet, thanks! 🙂

loopCount: I set it to if loopCount = 5, but it doesn't even try once before giving an error -128. I want it to telnet 192.168.1.1 say 5 times before the script ends/fails.

Thanks for everything so far. You've been great help! 🙂

Aug 12, 2008 9:12 AM in response to 3zzy

Okay, this seems to work:

tell application "Terminal"
do script "telnet 192.168.1.1" -- this opens a new window
set wID to id of front window -- grab the new window's ID
set loopCount to 0
repeat until (last word of (contents of window id wID as text) = "password")
set loopCount to loopCount + 1 -- increment the counter
if loopCount = 5 then say "Reboot failed!"
delay 2
do script "telnet 192.168.1.1" in front window
end repeat
do script "mypassword" in window id wID
delay 2
do script "set reboot" in window id wID
say "Successfully Rebooted!"
end tell

It says "reboot failed" if it doesn't reach till the password prompt, but it doesn't quit there, it still completes the next commands. How'd I stop the script after it says "Reboot Failed"?

Thanks.

Message was edited by: 3zzy

Aug 13, 2008 11:22 AM in response to 3zzy

How'd I stop the script after it says "Reboot Failed"?


You can either call:

return


which tells your script to exit the current handler (or quit if you're in the run handler), or you can:

error -128


which initiates a 'user cancelled' response (has the same basic effect, but may be 'more' correct since it indicates that the script did not compete normally).

Aug 13, 2008 11:51 AM in response to Camelot

Can you please see if this looks right:

tell application "Terminal"
do script "telnet 192.168.1.1" -- this opens a new window
set wID to id of front window -- grab the new window's ID
set loopCount to 0
repeat until (last word of (contents of window id wID as text) = "password")
set loopCount to loopCount + 1 -- increment the counter
if loopCount = 5 then return (say "reboot failed") quit
delay 2
do script "telnet 192.168.1.1" in front window
end repeat
do script "mypassword" in window id wID
delay 2
do script "set reboot" in window id wID
say "Successfully Rebooted!"
end tell

..it does seem to stop retries after 5, says "reboot failed" and quits, but just want to confirm.

Many thanks!

Aug 13, 2008 12:32 PM in response to 3zzy

The only part that's wrong here is the line:

if loopCount = 5 then return (say "reboot failed") quit


which may be a formatting error on the boards, or it may be how you entered the script.

Normally I'd just say:

if loopCount = 5 then
say "reboot failed"
return
end if


If you use the command 'quit', it will tell Terminal.app to quit, which may or may not be what you want.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript/Telnet Help

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.