Jump to content
31290

Restart a function

Recommended Posts

31290

Hi guys, 

Hope you are fine today :)

I'm trying to restart a function by calling it back in the script:

Here's my code so far:

Func f_VPN()
    $iInputBox = InputBox("Password", "Please Enter the User's password.")
    If @Error = 1 Then 
            GUICtrlSetState($fVPN, $GUI_UNCHECKED)
    Else
        ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe")
        $hVPN = WinWait("Cisco AnyConnect Secure Mobility Client", "Ready to connect")
        ControlClick($hVPN, "", "Button1")
        $hVPN1 = WinWait("Cisco AnyConnect | SEE VPN", "Cancel")
        ControlSetText($hVPN1, "", "Edit2", @Username)  
        Sleep(250)
        ControlSetText($hVPN1, "", "Edit3", $iInputBox)
        Sleep(250)
        ControlClick($hVPN1, "", "Button1")
        Sleep(5000)
        If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Login Failed" Then 
            ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button4")
            WinClose($hVPN)
            f_VPN()
        Else
        ShellExecute("C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe")
        If ControlGetText("Cisco AnyConnect Secure Mobility Client", "", "Static2") = "Connected To SEE VPN." Then ControlClick("Cisco AnyConnect Secure Mobility Client", "", "Button1")
        Sleep(1000)
        WinClose("Cisco AnyConnect Secure Mobility Client", "")
        ProcessClose("vpnui.exe")
        ; ProcessClose("explorer.exe") <<< TO UNCOMMENT
        IniWrite($oIniFile, "LaptopChkBox", "VPN", "1")
        EndIf
    EndIf   
EndFunc

I'd like to the function to be restarted in the case the password provided is not correct. 
I've googled many things but nothing very relevant.

Any ideas over here?

Thanks :)

-31290

 


~~~ Doom Shall Never Die, Only The Players ~~~

Share this post


Link to post
Share on other sites
SadBunny

How about breaking things up into nice separate functions so your program flow is much easier to control/steer?

Something like this (with a lot of pseudocode that you can fill in yourself :) ):

Global $MAX_PWD_FAIL_COUNT = 3

; main script
If Not fireItUp() Then
    MsgBox(16, "error", "something went wrong, you probably got an earlier error about this")
Else
    MsgBox(64, "all good", "vpn logged in!")
EndIf

Exit


Func fireItUp()

    $clientGui = startClient()
    If Not $clientGui Then
        MsgBox(64, "failure to start client", "failed")
        Return False
    EndIf

    $failCount = 0
    While Not loginClient($clientGui)
        $failCount += 1
        If $failCount > $MAX_PWD_FAIL_COUNT Then
            MsgBox(16, "failure to login client", "failed " & $MAX_PWD_FAIL_COUNT & " times! Cancelling.")
            ; break down gui, kill process, whatever
            cleanupAfterFailedLogin()
            ; Return False, so the calling code can tell that stuff went wrong
            Return False
        EndIf
    WEnd

    ; If we get here, we know that everything was ok
    cleanupAfterCorrectLogin()

    Return True ;
EndFunc   ;==>fireItUp

Func startClient()
    ; Start client
    ; Return $hVPN1 if success, False if failure
EndFunc   ;==>startClient

Func loginClient($clientGui)
    ; Do login stuff using $clientGui
    ; Wait for window to reflect success (then return True) or failure (then return False)
EndFunc   ;==>loginClient

Func cleanupAfterCorrectLogin()
    cleanupGeneral()
    ; ini write, log write, whatever specific for handling correct login
EndFunc   ;==>cleanupAfterCorrectLogin

Func cleanupAfterFailedLogin()
    cleanupGeneral()
    ; ini write, log write, whatever specific for handling failed login
EndFunc   ;==>cleanupAfterFailedLogin

Func cleanupGeneral()
    ; gui process/windows close, ini write etc., anything that needs to be done whether everything worked or not
EndFunc   ;==>cleanupGeneral

 

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
31290

Thanks for your input SadBunny.

Thing is that this "VPN" function (like many other function present in one file) is called when  a tech is clicking on a checkbox coming from a main GUI. Then  the tech have to enter the end user's password so the VPN connection can be tested. 

 

$gMainGUI = GUICreate("SEE CHECKLISTS", 333, 500)
    GUISetBkColor($Color_White)
    GUISetFont(8.5, 700, 0)
    ;GUICtrlCreatePic($sResources & "SAClogo.jpg", 75, 27, 183, 59)
    ;GUICtrlCreateLabel("SEE CHECKLISTS", 120, 114)
    GUICtrlSetColor(-1, 0x800080)
    ;GUICtrlCreateSeperator(0, 0, 135, 2, 390)
    GUICtrlCreateLabel("[ Current Computer name: " & @ComputerName & " ]", 50, 151, 324, 17)    
    GUICtrlSetColor(-1, $COLOR_RED)
    ;GUICtrlCreateSeperator(0, 0, 181, 2, 390)
    $fVPN = GUICtrlCreateCheckbox("Test the VPN connection", 5, 391)
    GUICtrlSetTip(-1, "Open Cisco and login with user's credentials")
    GUICtrlSetOnEvent($fVPN, "f_VPN")
    GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
    GUISetState(@SW_SHOW)

If login is OK, then go on with script. But if Login failed, VPN process test has to be launched one or two more times. That's why I really need the VPN function to be recalled within the function.


~~~ Doom Shall Never Die, Only The Players ~~~

Share this post


Link to post
Share on other sites
Subz

I believe the issue is with your Window naming, I can get it to work fine, if I change the ControlGetText and ControlClick "Title" and "ClassNN" after the "Sleep(5000)" line, I also removed the WinClose as I couldn't see the use of this.  I'm not sure why you can't just get the Tech to type the Password into the VPN Client though, especially since the Password window stays open after it fails.  You could just check once VPN has successfully logged in before updating your ini file.

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

  • Similar Content

    • omicron
      By omicron
      Hello!

      I am working on a function that I am just getting lost on. The goal is a multiple nested loop.

      Here are the steps:
      Contents of file1.txt::
      [topic] var1=Name var2=OtherName var3=SomeotheName Contents of file2.txt::
      [subTopic] top=sub1 top2=sub2 top3=sub3 The Shell I am working from::
      #include <file.au3> $file = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) msgbox(0,'','the line ' & $i & ' is ' & $line) Next FileClose($file) Understanding however that the "msgbox" needs to then become a variable. in example the following::
      $file = "c:\yourfile.txt" FileOpen($file, 0) While true( prog.exe is running && "WinName" is open) do For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ;Open File to log "current location of file 1" FileWriteLine ("filename", $i & ' is ' & $line) var = $line Next $file2 = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ; OpenFile to log "Current location of file 2" FileWriteLine ("filename", $i & ' is ' & $line) Next FileClose($file2) FileClose($file) The goal in written form is the following ::

      While in "OpenWindow"
          read from file 1 starting at line 1 until end of file.
         file 1 is a list of names to be searched.
         With $line selected, add this element to the element in file 2.
       
      The search of a variables in list 1 and list 2 differ on the amount of posts that day. (This is not a web based platform, it is a game) I need to search 2 names and take a screenshot of the out put. The sizes of the names list depend on the activity of names at the time of search.
      This loop continues until all the names from both lists have been searched. Mostly in the format of::
      File1= item
      File2= Vendor
       
      Item + Vendor  ( Capture screen, scroll) -- Not sure how to detect if I need to scroll)
       
      Thank you for your help and support!
    • Jackized
      By Jackized
      Is there a way to loop back to the beginning and keep the already entered data in the GUI Input boxes? I can GUIDelete and start a new GUI but all the typed data has to be redone. Here is a sample.

       
      #Include <GuiConstants.au3> Dim $msg,$okbutton,$cancelbutton,$objOU,$intAccValue,$strGroup1,$strGroup2 Dim $strFirstName,$strLastName,$strUserName,$strUserName2,$strPassword,$strDescription,$strContainer Dim $Combo,$Duration Do $Form1 = GuiCreate("", 300, 300)          GUISetBkColor(0xA6CAF0)         GuiCtrlCreateLabel("First Name:", 50, 10)         $strFirstName = GuiCtrlCreateInput("", 50, 25, 200, 20)         GUICtrlSetState ( $okbutton, $GUI_FOCUS )         GuiSetState()     $okbutton = GUICtrlCreateButton ("OK",75,240,50,20)     $cancelbutton = GUICtrlCreateButton ("Cancel",175,240,50,20) While 1     $msg = GUIGetMsg()     Select     Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton             Exit     Case $msg = $okbutton             $strFirstName = GUICtrlRead($strFirstName)             ExitLoop     EndSelect Wend $len = StringLen($strFirstName)     If $len > "20" then         MsgBox(0, "ERROR Name to long", $strFirstName & " can't be over 20 characters." & @CRLF & "Total: " & $len)         ;Exit         GUIDelete($Form1)     EndIf Until $len < "20" MsgBox(0, "Done", "")  
    • mrtgtr
      By mrtgtr
      I want to do something , after wait 2 min and again do same thing
      But I do do not want with sleep, it must be timer 
      How I do this ?
    • Emmhor1
      By Emmhor1
      Hi All,

      MAIN QUESTION:
      Is it possible to Call specific function within a GUI

      So I have a script with multiple functions although I don't want to use every function every time.
      My Idea is to create a simple GUI which allows me to select what functions I want to use then run the funtions by clicking a button.
      I have already made a GUI which allows me to select specific .exe's I would like to run after selection it runs the .exe one by one.
      This script is on my work laptops and cannot access it right now.
       
      Who can help me with this?
      GUIcreate
      Func1 
      Func2
      Func3
      Then have a boxes which allows me to select the specif Func.(I used GUIChecked and Unchecked in my other script)
      Then a button which executes/calls the selected functions
    • Skeletor
      By Skeletor
      Hi Guys,
      Is it possible to get a variable on your For..Next loop? 
       
      Local $Lines1 = _FileCountLines(C:\temp\test.txt) Local $linesToCount2 = $Lines1 + 2 $var = Number($linesToCount2) For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = $var To $count Next ;Code does stuff here Next Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.
      This code I posted above does not move to the next value.
      But the code below does... why is that?
      For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = 2 To $count Next ;Code does stuff here Next  
      Why is the For loop resetting itself?
      Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?
      If so , how would you make this variable static?

       
×