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

sundiver

macrumors newbie
Original poster
Dec 31, 2007
6
0
I am an absolute Mac and programming novice, though I can find my way around my laptop. I want to write a small applescript/automator action that will reboot my broadband router/modem. I have tried manually executing the following commands directly on the Mac Terminal and it works. I need some help porting this into applescript and have no idea how to do this... my commands are highlighted in bold below

Last login: Mon Dec 31 18:01:32 on ttys000
user-macbook-pro:~ user$ telnet 192.168.1.1 23 <return>
Trying 192.168.1.1.. .
Connected to 192.168.1.1.
Escape character is '^]'.

*******************
Welcome to Vulcan
*******************

Conexant Inc., Software Release R100B03.0A.M
Copyright (c) 2001-2003 by Conexant, Inc.

login: admin <return>
password: xxxxx <return>
Login Successful
$reboot <return>

Mon Dec 31 18:02:55 2007 : WARNING : PPP Interface Down : Interface - ppp-0

I do not know how to terminate terminal thereafter. Would you please be able to help me out here by telling me what I should do to automate this simple action?

One more thing, though... I believe I will need a 2 to 5 second break in between each command just to give the computer time to log into the router, so the script will need to be modified accordingly.

Thanks very much!
 

sundiver

macrumors newbie
Original poster
Dec 31, 2007
6
0
Thanks. I just tried running this script I wrote below, and while it calls up the Terminal program and types out telnet 192.68.1.1 23 in the Terminal window, nothing else seems to happen! Any idea why? Are there any syntax errors in my script please?

tell application "Terminal"
activate
delay 2
do script ("telnet 192.68.1.1 23") in front window
delay 5
do script ("admin") in front window
delay 5
do script ("admin") in front window
delay 5
do script ("reboot") in front window
quit
end tell
 

lancestraz

macrumors 6502a
Nov 27, 2005
898
0
RI
Thanks. Are there any syntax errors in the attached script please?

tell application "Terminal"
activate
do shell script ("telnet 192.68.1.1 23")
delay 5
do shell script ("admin")
delay 5
do shell script ("password")
delay 5
do shell script ("reboot")
quit
end tell
You don't need to include "tell application "Terminal"; quit; end tell.
The do shell script command doesn't need Terminal open to work.
However, I'm not sure "do shell script" will work in this case. Try it and see.
 

sundiver

macrumors newbie
Original poster
Dec 31, 2007
6
0
Nope, if I don't call out the application, I cannot telnet into the router. But when I do telnet into the router using the script, I get no response.

Seems to work fine when I type out the commands in terminal though!
 

lancestraz

macrumors 6502a
Nov 27, 2005
898
0
RI
Nope, if I don't call out the application, I cannot telnet into the router. But when I do telnet into the router using the script, I get no response.

Seems to work fine when I type out the commands in terminal though!
Yeah, I thought that might happen.

I'm not sure if it will let you enter a password using the keystroke command but try this.
Code:
tell application "Terminal" to activate
delay 2
tell application "System Events"
	tell process "Terminal"
		keystroke "telnet 192.168.1.1 23"
		keystroke return
		delay 5
		keystroke "admin"
		keystroke return
		delay 5
		keystroke "password"
		keystroke return
		delay 5
		keystroke "reboot"
		keystroke return
	end tell
	quit
end tell
 

sundiver

macrumors newbie
Original poster
Dec 31, 2007
6
0
Thank you. That worked beautifully. One last bit of advice: the script does not end the terminal session and the terminal window stays open. Is there something that needs to be added/ modified to get Terminal to quit?

I inserted
tell application "Terminal" to quit
end tell

insted of the vanilla quit command towards the end but got a compile error.

Should I use this code for quitting Terminal (add this after the quit command in lancestraz's code)? Thanks!
on quit
continue quit
end quit
 

prostuff1

macrumors 65816
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
Thank you. That worked beautifully. One last bit of advice: the script does not end the terminal session and the terminal window stays open. Is there something that needs to be added/ modified to get Terminal to quit?

I inserted
tell application "Terminal" to quit
end tell

insted of the vanilla quit command towards the end but got a compile error.

Should I use this code for quitting Terminal (add this after the quit command in lancestraz's code)? Thanks!
on quit
continue quit
end quit

I just did some messing around and this code


Code:
tell application "Terminal"
	quit
end tell
works to quit the terminal

Hope that helps
 

lancestraz

macrumors 6502a
Nov 27, 2005
898
0
RI
Thank you. That worked beautifully. One last bit of advice: the script does not end the terminal session and the terminal window stays open. Is there something that needs to be added/ modified to get Terminal to quit?

I inserted
tell application "Terminal" to quit
end tell

insted of the vanilla quit command towards the end but got a compile error.

Should I use this code for quitting Terminal (add this after the quit command in lancestraz's code)? Thanks!
on quit
continue quit
end quit
Oops. My bad. I messed up my tell commands. Use this code instead.
Code:
tell application "Terminal"
	activate
	delay 1
	tell application "System Events"
		tell process "Terminal"
			keystroke "telnet 192.168.1.1 23"
			keystroke return
			delay 5
			keystroke "admin"
			keystroke return
			delay 5
			keystroke "password"
			keystroke return
			delay 5
			keystroke "reboot"
			keystroke return
		end tell
	end tell
	quit
end tell
 

sundiver

macrumors newbie
Original poster
Dec 31, 2007
6
0
Reboot MTNL ADSL router

Thanks so much. That works like a dream.

The final script (for anyone else who wants to use it) is below. I use a UT starcom UT-304 R2 router:

ell application "Terminal"
activate
delay 1
tell application "System Events"
tell process "Terminal"
keystroke "telnet 192.168.1.1 23"
keystroke return
delay 5
keystroke "admin"
keystroke return
delay 5
keystroke "admin"
keystroke return
delay 5
keystroke "reboot"
keystroke return
end tell
end tell
quit
end tell
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.