Link to home
Start Free TrialLog in
Avatar of lhutton
lhutton

asked on

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:\.
Avatar of gimosuby
gimosuby

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

Avatar of lhutton

ASKER

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.
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")
Avatar of lhutton

ASKER

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.
Try to do a search for telnet.exe. And once you found it, use the path to it.
Did you get this working?
Avatar of lhutton

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of gimosuby
gimosuby

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lhutton

ASKER

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

Put this:


Opt("WinTitleMatchMode", 2)

at the top of the script.
Avatar of lhutton

ASKER

This works; thank you!