Reboot ADSL router script

Discussion in 'Mac Programming' started by sundiver, Dec 31, 2007.

  1. sundiver macrumors newbie

    Joined:
    Dec 31, 2007
    #1
    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!
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    The Applescript command:delay n will wait for n seconds.
     
  3. sundiver thread starter macrumors newbie

    Joined:
    Dec 31, 2007
    #3
    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
     
  4. lancestraz macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #4
    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.
     
  5. sundiver thread starter macrumors newbie

    Joined:
    Dec 31, 2007
    #5
    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!
     
  6. lancestraz macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #6
    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
     
  7. sundiver thread starter macrumors newbie

    Joined:
    Dec 31, 2007
    #7
    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
     
  8. prostuff1 macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #8
    I just did some messing around and this code


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

    Hope that helps
     
  9. lancestraz macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #9
    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
     
  10. sundiver thread starter macrumors newbie

    Joined:
    Dec 31, 2007
    #10
    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
     

Share This Page