Jump to content

need help trying to combine these two scripts,


urlci
 Share

Recommended Posts

Hello,

First of all i'm new to this stuff, pardon my ignorance. i am trying to automate "modem reboot" with autoit. i've been searching the forums for a while and i guess i got all the "ingredients" to create what i want. however after my many failed attempts i couldn't manage to do it. here is what i am trying to do;

i have a tplink modem which is quite bad and i download stuff from internet daily. sometimes i leave the pc on nights, but wake up to disconnects/ freezes which ruins the whole purpose. so i execute this script with windows 7 task scheduler every hour. it works fine and does the job for me %80 of the time.

#include <IE.au3>

Local $oUser, $oPass, $oSubmit
Local $sUser = "admin"
Local $sPass = "admin"
Local $url = "192.168.1.1"
Local $oIE = _IECreate($url, 0,1)
Local $URLRestart = "http://192.168.1.1/#__restart.htm"

Logout()

Func Riavvio()
$o_login = _IEGetObjById($oIE,"userName")

_IEFormElementSetValue ($o_login, $sUser)
$o_password = _IEGetObjByID($oIE,"pcPassword")
_IEFormElementSetValue ($o_password, $sPass)

$o_signin=_IEGetObjById($oIE,"loginBtn")
_IEAction($o_signin,"click")

_IENavigate($oIE, $URLRestart)
$o_reboot=_IEGetObjById($oIE,"button_reboot")

sleep(2000)

_IEAction ($o_reboot, "focus")
ControlSend(_IEPropertyGet($Oie, "hwnd"), "", "", "{ENTER}");
sleep(2000)
WinWaitActive("[CLASS:#32770]", "")
sleep(2000)
ControlClick ("[CLASS:#32770]", "OK", 1, "left", 1)
_IELoadWait($oIE)
sleep(2000)

EndFunc



Func Logout()

   Local $oLogout = _IEGetObjByID($oIE, "menu_logout")
   if @error Then
      Riavvio()
   else

   Local $hWnd = _IEPropertyGet($oIE, "hwnd")
   _IEAction($oLogout, "focus")
      ControlSend($hWnd, "", "", "{Enter}")

   WinWaitActive("[CLASS:#32770]", "")
   ControlClick ("[CLASS:#32770]", "OK", 1, "left", 1)
EndIf

sleep(100)
Riavvio()

EndFunc

But the other %20 of the time, the connection looks timed out (?). network sharing center says i am connected. also modem interface shows the same. but i can't connect any site, login/logout via any program (steam/skype etc.). the connection is just dead. In that case i am needing another stage in the script to detect the failed connection to perform another reboot. so i found this one which works alone to tell me if i am connected or not.

#AutoIt3Wrapper_icon=1.ico
#include <WinAPIDiag.au3>
While 1
    If _WinAPI_IsInternetConnected() Then
        MsgBox("", "Internet", "You are connected")
        ExitLoop
    EndIf
    Sleep(1000) ;
WEnd

I tried to combine these two, but simply couldn't make it work. most of my attempts ended with errors and some others are just performing imperfect. Should i gave up on task scheduler and do something with "WHILE" that repeats the main reboot code? Or insert the reboot code into connection checker? How am i supposed to make it work non-stop?

Link to comment
Share on other sites

Hi @urlci, welcome to autoit forum.

Let me see if I can help you out.  Since I cannot replicate the action of your script, I will need you to answer a few questions.  If I understand correctly, the first script logout your modem, then relogin just after.  You have put it in task scheduler and execute this logout/login every hour. Right ?

How do you make sure there is no task running at the time of your logout/login ?

When the "disconnects/ freezes" happen,  does the second script tell you that you are no longer connected to internet ?

How do you restart the jobs that were undergoing when you disconnected ?

Am I right to say that you would like the script to check if you just got disconnected, and if so, perform a logout/login reboot ?  If we could do that then, you could get rid of the task scheduler approach ?

Edited by Nine
Link to comment
Share on other sites

Hi! Thanks in advance.

1- Correct. Even if i am pre-logged in, it runs the internet explorer, logs out. I immediately see the login screen. It fills the username and password and does the rest of the stuff. Yes , I've put it on a 1-2 hour interval. So scheduler runs the au3 file every few hours and resets the modem, but as i said sometimes i get dead connections. I can see my ip through modem UI, but can't surf any website, or ping.

2- I don't understand. If you are talking about running a script after another, they don't interfere with each other. They just stack on system tray and I terminate them in the morning. I got no problem so far.

3- I modified it like this 

#AutoIt3Wrapper_icon=1.ico
#include <WinAPIDiag.au3>
While 1
    If _WinAPI_IsInternetConnected() Then
        MsgBox("", "Internet", "You are connected")
        ExitLoop
    else
    MsgBox(0, "internet", "not connected")
    EndIf
    Sleep(1000) ;
WEnd

and it tells me if the connection is not present. However I didn't have an opportunity to test it on one of the "frozen" weird connections. I have doubts that it will work, however it can be solved with other methods like ping, $iBytesRead or other stuff? I've seen some other ways to check the internet status with autoit. Like;

while 1 
    Local $dData = InetRead("http://www.autoitscript.com/autoit3/files/beta/update.dat")

    Local $iBytesRead = @extended

    Local $sData = BinaryToString($dData)
If $dData = "" Then
    Sleep( 1000 )
    MsgBox(0, "internet", "connected")
Else
    MsgBox(0, "internet", "not conected") !!THIS PART DONT WORK FOR SOME REASON!!
    Endif
WEnd

or 

#include <MsgBoxConstants.au3>
#include <INet.au3>
Global $dData
While 1
    $dData = _GetIP()
    MsgBox($MB_SYSTEMMODAL, "", "Your external IP address is: " & $dData)
    If $dData <> -1 Then ;  internet connection is working
        MsgBox($MB_SYSTEMMODAL, "", "Your external IP address is: " & $dData)
    EndIf
    Sleep(3000) ; sleep for 5 minutes (and one second)
WEnd

...

The good thing about the first script is, it tells me if the connection is not available. I can add some functions with ELSE statement. Other examples just verify the established connection, not if it is gone/cut. And for some reason I can't add any functions with ELSE. 

Link to comment
Share on other sites

1 hour ago, urlci said:

2- I don't understand. If you are talking about running a script after another, they don't interfere with each other. They just stack on system tray and I terminate them in the morning. I got no problem so far.

Not other scripts, I mean jobs you are doing like uploading, downloading files, or whatever jobs you do over internet...How do you know there is no jobs going on ? How do you restart them if they failed ?

Anyway here a way to combine all your scripts...

#include <INet.au3>
#include <WinAPIDiag.au3>

Opt ("MustDeclareVars", 1)

HotKeySet("{END}", "Terminate")

While True
    If not _WinAPI_IsInternetConnected() Then ContinueLoop Logout ()

    InetRead("http://www.autoitscript.com/autoit3/files/beta/update.dat", $INET_FORCERELOAD)
    if @error then ContinueLoop Logout ()

    _GetIP()
    If @error Then ContinueLoop Logout ()

    ; if all test are successful, then sleep for 5 mins
    Sleep(5 * 60 * 1000)
WEnd

Func Logout ()

    MsgBox (0,"","Not working") ; for testing purpose

; your code goes here

    Return 1 ; must be there for loops to continue in main script flawlessly
EndFunc

Func Terminate ()

    Exit

EndFunc

 

Edited by Nine
Link to comment
Share on other sites

Connection cheeking is working flawlessly, it detects both connection cuts and freezes. however the rebooter gives error;

$o_login = _IEGetObjById($oIE,"userName")

$o_login = _IEGetObjById(^ERROR

error: variable used without being declared

 

it is working alone, but when i combine with yours it gives the error. why?

#include <INet.au3>
#include <WinAPIDiag.au3>
#include <IE.au3>
#AutoIt3Wrapper_icon=1.ico
Opt ("MustDeclareVars", 1)

HotKeySet("{END}", "Terminate")

While True
    If not _WinAPI_IsInternetConnected() Then ContinueLoop Logout ()

    InetRead("http://www.autoitscript.com/autoit3/files/beta/update.dat", $INET_FORCERELOAD)
    if @error then ContinueLoop Logout ()

    _GetIP()
    If @error Then ContinueLoop Logout ()

    ; if all test are successful, then sleep for 5 min
    Sleep(300000)
WEnd

Func Riavvio()
$o_login = _IEGetObjById($oIE,"userName")

_IEFormElementSetValue ($o_login, $sUser)
$o_password = _IEGetObjByID($oIE,"pcPassword")
_IEFormElementSetValue ($o_password, $sPass)

$o_signin=_IEGetObjById($oIE,"loginBtn")
_IEAction($o_signin,"click")

_IENavigate($oIE, $URLRestart)
$o_reboot=_IEGetObjById($oIE,"button_reboot")

sleep(2000)

_IEAction ($o_reboot, "focus")
ControlSend(_IEPropertyGet($Oie, "hwnd"), "", "", "{ENTER}");
sleep(2000)
WinWaitActive("[CLASS:#32770]", "")
sleep(2000)
ControlClick ("[CLASS:#32770]", "OK", 1, "left", 1)
_IELoadWait($oIE)
sleep(2000)

EndFunc

Func Logout()

Local $oUser, $oPass, $oSubmit
Local $sUser = "admin"
Local $sPass = "admin"
Local $url = "192.168.1.1"
Local $oIE = _IECreate($url, 0,1)
Local $URLRestart = "http://192.168.1.1/#__restart.htm"

   Local $oLogout = _IEGetObjByID($oIE, "menu_logout")
   if @error Then
      Riavvio()
   else

   Local $hWnd = _IEPropertyGet($oIE, "hwnd")
   _IEAction($oLogout, "focus")
      ControlSend($hWnd, "", "", "{Enter}")

   WinWaitActive("[CLASS:#32770]", "")
   ControlClick ("[CLASS:#32770]", "OK", 1, "left", 1)
EndIf

sleep(3000)
Riavvio()

EndFunc

Func Terminate ()

    Exit

EndFunc

 

 

 

Link to comment
Share on other sites

Opt ("MustDeclareVars", 1)

Every variables must be declared Local (or Global if required).  It is a good practice to have this opt...

In your particular case, there is no need to split your code into 2 functions, just grab riavvio code and insert it at the end of logout.  All variables are declared in logout but not in riavvio !

Edited by Nine
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...