AutoIt Script to Reboot Router

I want to create a script that:
1. opens a telnet connection to my router, allowing for a username and password;
2. reboots my router;
3. adds a counter to the command prompt window, counting down from 50; and
4. closes the command prompt.

The script needs to work when run on a local drive other than C:\.
lhuttonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gimosubyCommented:
The code below should do the trick.

If you want a script that would run without using the 'WinActivate' and 'send' commands, check out this post: http://www.autoitscript.com/forum/index.php?showtopic=36206
run(@ComSpec & " /c" & "telnet 192.168.1.254","")
WinWait('Telnet 192.168.1.254')
WinActivate("Telnet 192.168.1.254")
send ("SuperUser{ENTER}")
sleep(1000)
WinActivate("Telnet 192.168.1.254")
send ("password{ENTER}")
sleep(1000)
WinActivate("Telnet 192.168.1.254")
send ("reload")
sleep(50000) ; Sleep 50 seconds
WinActivate("Telnet 192.168.1.254")
send ("q{ENTER}")
WinClose("Telnet 192.168.1.254")
Exit

Open in new window

lhuttonAuthor Commented:
Thank you. However, I'm not able to run the script unless it's located in the same location as telnet; is there a workaround to allow me to run the script regardless where it's located, please?

Also, I am wanting to add a visible counter to the window, so I can monitor progress.
gimosubyCommented:
It should run from anywhere as telnet is installed by default in the system32 folder.
You could replace line:
run(@ComSpec & " /c" & "telnet 192.168.1.254","")
with:
run(@ComSpec & " /c" & @SystemDir & "\telnet 192.168.1.254","")
or
run(@ComSpec & " /c" &  "<path to telnet>\telnet 192.168.1.254","")

As for the 50 second countdown, download ftp://ftp.simtel.net/pub/simtelnet/msdos/batchutl/waiter.zip
Put waiter.exe into the same directory and replace 'sleep(50000) ; Sleep 50 seconds'
with:

WinActivate("Telnet 192.168.1.254")
send ("waiter.exe -qs50")
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

lhuttonAuthor Commented:
I still can't get the script to work unless (presumably) it's located in C:\Windows\System32, where telnet resides; @SystemDir wrongly points to C:\Windows\SysWOW64, and even specifying the full path to telnet - as you suggested - doesn't work.
gimosubyCommented:
Try to do a search for telnet.exe. And once you found it, use the path to it.
gimosubyCommented:
Did you get this working?
lhuttonAuthor Commented:
I know where telnet.exe is: the issue is that when I try to run the script outside C:\Windows\System32, it fails because it is expecting telnet.exe to be in the same folder. See http://www.autoitscript.com/forum/index.php?showtopic=113538 for a discussion of the issue and, I believe, a potential solution in post #14; I'm just not certain how to implement it.
gimosubyCommented:
Try the code below. You could also try to copy telnet.exe into the script directory.
_Wow64FsRedirection(True)
run(@ComSpec & " /c" & "telnet 192.168.1.254","")
WinWait('Telnet 192.168.1.254')
WinActivate("Telnet 192.168.1.254")
send ("SuperUser{ENTER}")
sleep(1000)
WinActivate("Telnet 192.168.1.254")
send ("password{ENTER}")
sleep(1000)
WinActivate("Telnet 192.168.1.254")
send ("reload")
sleep(50000) ; Sleep 50 seconds
WinActivate("Telnet 192.168.1.254")
send ("q{ENTER}")
WinClose("Telnet 192.168.1.254")

Func _Wow64FsRedirection($state)
    ; Disables or reverts the filesystem redirector for a 32 bit process running on 64bit OS
    If Not @AutoItX64 And @OSArch = 'X64' Then
        If $state Then
            DllCall("kernel32.dll", "int", "Wow64RevertWow64FsRedirection", "int", 0)
        Else
            DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 0); or 1 as per help
        EndIf
        If @error Then Return SetError(1)
    EndIf
Exit

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lhuttonAuthor Commented:
I had to copy telnet.exe into the script directory as you suggested to get it to work, and had to remove "Telnet" from the window title, as it only displays the IP address. However, the script stops at "Login name:".
_Wow64FsRedirection(True)
run(@ComSpec & " /k" & "telnet 192.168.1.1","")
WinWait("192.168.1.1")
WinActivate("192.168.1.1")
send ("admin{ENTER}")
sleep(1000)
WinActivate("192.168.1.1")
send ("password{ENTER}")
sleep(1000)
WinActivate("192.168.1.1")
send ("reboot")
sleep(50000) ; Sleep 50 seconds
WinActivate("192.168.1.1")
send ("q{ENTER}")
WinClose("192.168.1.1")

Func _Wow64FsRedirection($state)
    ; Disables or reverts the filesystem redirector for a 32 bit process running on 64bit OS
    If Not @AutoItX64 And @OSArch = 'X64' Then
        If $state Then
            DllCall("kernel32.dll", "int", "Wow64RevertWow64FsRedirection", "int", 0)
        Else
            DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 0); or 1 as per help
        EndIf
        If @error Then Return SetError(1)
    EndIf
EndFunc
Exit

Open in new window

gimosubyCommented:
Put this:


Opt("WinTitleMatchMode", 2)

at the top of the script.
lhuttonAuthor Commented:
This works; thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.