Jump to content
youtuber

How to restart my modem with telnet

Recommended Posts

Jos

Which part do you think failed as you aren't doing any error checking and simply throwing data to the router at full speed?
Try adding some error checking to see whether each step is successfull before doing the next step... and maybe a pause here and there helps too.

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
ripdad

Also, each command you send must end with @LF or @CRLF

 

  • Like 1

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
youtuber

I do not know if there is a mistake in my spelling I do not know about tcp

Global $err = @error
TCPStartup()
$Connect = TCPConnect("192.168.1.1", 23)
If $Connect = -1 Then
    ConsoleWrite($Connect & @crlf)
    Exit
EndIf

Sleep(500)
$recv = TCPRecv($Connect, 500)

If $recv <> "" then
ConsoleWrite('Received: ' & $recv & @crlf)
else
ConsoleWrite('got nothing? ' & $recv & ":: " & $err & @CRLF)
EndIf

TCPSend($Connect, "root" & @CRLF)

$recv2 = TCPRecv($Connect, 500)

If $recv2 <> "" then
ConsoleWrite('Received2: ' & $recv2 & @crlf)
else
ConsoleWrite('got nothing? ' & $recv2 & ":: " & $err & @CRLF)
EndIf

TCPSend($Connect, "password" & @CRLF)

$recv3 = TCPRecv($Connect, 500)

If $recv3 <> "" then
ConsoleWrite('Received3: ' & $recv3 & @crlf)
else
ConsoleWrite('got nothing? ' & $recv3 & ":: " & $err & @CRLF)
EndIf

TCPSend($Connect, "reboot" & @CRLF)

$recv4 = TCPRecv($Connect, 500)

If $recv4 <> "" then
ConsoleWrite('Received4: ' & $recv4 & @crlf)
else
ConsoleWrite('got nothing? ' & $recv4 & ":: " & $err & @CRLF)
EndIf

TCPCloseSocket($Connect)

TCPShutdown()

My console output

Spoiler

Screenshot_2.jpg

 

Share this post


Link to post
Share on other sites
ripdad

Should be close enough -- Not tested.

Tested with my router...

Opt('MustDeclareVars', 1)
;
If Not TCPStartup() Then
    ConsoleWrite('Error: TCPStartup' & @CRLF)
    Exit
EndIf
;
Local $gui = GUICreate('', 400, 400)
Local $edt = GUICtrlCreateEdit('', 10, 10, 380, 380)
GUISetState(@SW_SHOW, $gui)
;
Local $nSocket = TCPConnect('192.168.1.1', 23); <- make sure ip and port are correct!
If @error Or $nSocket < 1 Then
    GUICtrlSetData($edt, 'Error: TCPConnect' & @CRLF, 1)
    Exit
Else
    GUICtrlSetData($edt, '-> Connected' & @CRLF, 1)
EndIf
;
Sleep(1000)
_Recv($nSocket)
;
Local $aCommand[4] = [3, 'root', 'password', 'reboot']; <- make sure commands are correct!
;
For $i = 1 To $aCommand[0]
    TCPSend($nSocket, $aCommand[$i] & @CRLF)
    If @error Then
        GUICtrlSetData($edt, 'Error: TCPSend' & @CRLF, 1)
        ExitLoop
    Else
        GUICtrlSetData($edt, '-> SEND: ' & $aCommand[$i] & @CRLF, 1)
        Sleep(1000)
        _Recv($nSocket)
        If @error Then
            ExitLoop
        EndIf
    EndIf
Next
;
GUICtrlSetData($edt, @CRLF & '-> EXIT' & @CRLF, 1)
Sleep(10000)
TCPCloseSocket($nSocket)
TCPShutdown()
Exit
;
Func _Recv($nSocket)
    Local $sRecv
    For $i = 1 To 25
        $sRecv = TCPRecv($nSocket, 2048, 1)
        If @error Then
            GUICtrlSetData($edt, 'Error: TCPRecv' & @CRLF, 1)
            Return SetError(1)
        ElseIf $sRecv <> '' Then
            GUICtrlSetData($edt, '-> RECEIVED: ' & BinaryToString($sRecv), 1)
            Return
        EndIf
        Sleep(10)
    Next
    GUICtrlSetData($edt, '-> No Response' & @CRLF, 1)
    Return
EndFunc
;

 

 

Edited by ripdad
  • Like 2

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
ripdad

So, I got around to testing with my router and found a few things amiss.

(1) the commands are different.

Instead of: root, password, reboot

It needed: login, password, restart

(2) the received data was in binary.

Changed to binary mode on TCPRecv() and added BinaryToString() for incoming data.

The script above has been modified to reflect these changes.

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
Bilgus

Its a lot better than trying to automate a telenet session but do be aware leaving telenet open on your router is a bad idea from a security perspective

Share this post


Link to post
Share on other sites
ripdad

Yep, I personally would not leave it that way on a business network. A home network might be okay.

Still nice to be able to do it, depending on circumstances.

Personally, I just unplug it, wait 10 seconds and plug it back in.

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×