Jump to content

Confused with If's Please help


Recommended Posts

I am trying to create (in fact its already working) little program that would continuously ping primary host to assure that it is connected to internet.

If server ping gets to 0 then it will write error into log file saying it did not get response from 1st server at @time @min.

Right after that it will (it should but it doesnt) ping second specified server and do same thing, if 2nd address ping is 0 then Error goes to log saying 2nd server no response.

Then it will start from the beginning from 1st server (loop) continuously writing log file.

Reason why i need this is i recently had frequent internet signal drops and i am trying to track down time when it happens so i could call Comcast and tell them when and for how long and how many times.

SO here is the code.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
FileDelete ("Log.txt")
GUICreate("",200,100)
GUISetState(@SW_SHOW)
$log = GUICtrlCreateEdit('',100,0,100,100)
$Addressinput = GUICtrlCreateInput("Primary host",0,0,100,20)
$Addressinput2 = GUICtrlCreateInput("Backup host",0,20,100,20)
$Timeinput = GUICtrlCreateInput("15000",0,40,40,20)
GUICtrlCreateLabel ("1000=1sec",40,43,60)
$Button = GUICtrlCreateButton ("Poke'em",0,60,100,40)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        case $Button
            While 1
$Address = GUICtrlRead($Addressinput)
$Address2 = GUICtrlRead($Addressinput2)
$Time = GUICtrlRead($Timeinput)
$ping = Ping ($Address)
GUICtrlSetData ($log,$ping & @CRLF)
Sleep ($Time)
If $ping <= 0 Then
    GUICtrlSetData ($log,"No response from 1st address at "&@HOUR &':'& @MIN & @CRLF)
    $output = GUICtrlRead ($log)
    FileWrite ("Log.txt",$output)
    MsgBox (0,'','1')
;Sleep ($time)
$ping2 = Ping ($Address2)
;GUICtrlSetData ($log,$ping2 & @CRLF)
    GUICtrlSetData ($log,"No response from 2nd address at "&@HOUR &':'& @MIN & @CRLF)
    $output2 = GUICtrlRead ($log)
    FileWrite ("Log.txt",$output2)
    MsgBox (0,'','2')
;Sleep ($time)
EndIf
WEnd

        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
exit

Problem with this code is that its not trying to connect to backup serer (HOST) to ping it and i am stuck trying to figure out best way for this to work.

Please help me solve it i am still new with these If's stuff.

Thanks

Link to comment
Share on other sites

  • Moderators

Gettingsmarter,

Take a look at this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{ESC}", "_On_Exit")
FileDelete("Log.txt")
GUICreate("", 400, 100)
GUISetState(@SW_SHOW)
$log = GUICtrlCreateEdit('', 100, 0, 300, 100)
$Addressinput = GUICtrlCreateInput("Primary host", 0, 0, 100, 20)
$Addressinput2 = GUICtrlCreateInput("Backup host", 0, 20, 100, 20)
$Timeinput = GUICtrlCreateInput("15000", 0, 40, 40, 20)
GUICtrlCreateLabel("1000=1sec", 40, 43, 60)
$Button = GUICtrlCreateButton("Poke'em", 0, 60, 100, 40)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button
            While 1
                $Address = GUICtrlRead($Addressinput)
                $Time = GUICtrlRead($Timeinput)
                $ping = Ping($Address, $Time)
                GUICtrlSetData($log, $ping & @CRLF)
                If $ping = 0 Then
                    GUICtrlSetData($log, "No response from 1st address at " & @HOUR & ':' & @MIN & @CRLF)
                    $output = GUICtrlRead($log)
                    FileWrite("Log.txt", $output)
                ; Try second server
                    $Address2 = GUICtrlRead($Addressinput2)
                    $ping2 = Ping($Address2, $Time)
                    If $ping2 = 0 Then
                        GUICtrlSetData($log, "No response from 2nd address at " & @HOUR & ':' & @MIN & @CRLF)
                        $output2 = GUICtrlRead($log)
                        FileWrite("Log.txt", $output2)
                    EndIf
                EndIf
            WEnd

        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
Exit

Func _On_Exit()
    Exit
EndFunc

I added another If section to cover the second Ping. I also increased the GUI size so I could see what was happening and gave you a Hotkey to exit (Escape). I think it should work for you now.

Ask if anything is unclear.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Gettingsmarter,

Are you sure? It works for me - just as the code says it will! Here is my Log.txt file:

No response from 1st address at 09:32
No response from 2nd address at 09:32
No response from 1st address at 09:32
No response from 2nd address at 09:32

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Gettingsmarter,

Are you sure? It works for me - just as the code says it will! Here is my Log.txt file:

No response from 1st address at 09:32
No response from 2nd address at 09:32
No response from 1st address at 09:32
No response from 2nd address at 09:32

M23

Something is wrong with ur code, no matter what time i set it keeps pinging them every have second.\Hosts will probably ban my IP after me doing it fir a while :D

Trying to figure out but it all looks good

Link to comment
Share on other sites

  • Moderators

Gettingsmarter,

My fault - I used your delay variable ($Timeinput) as the time out for the ping itself and not the delay between them. How much of a gap would you like between the 2 ping attempts and before retrying?

M23

Edit: This code waits for the set delay between each ping attempt. I hope it will save your connection! :-)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{ESC}", "_On_Exit")
FileDelete("Log.txt")
GUICreate("", 400, 100)
GUISetState(@SW_SHOW)
$log = GUICtrlCreateEdit('', 100, 0, 300, 100)
$Addressinput = GUICtrlCreateInput("Primary host", 0, 0, 100, 20)
$Addressinput2 = GUICtrlCreateInput("Backup host", 0, 20, 100, 20)
$Timeinput = GUICtrlCreateInput("15000", 0, 40, 40, 20)
GUICtrlCreateLabel("1000=1sec", 40, 43, 60)
$Button = GUICtrlCreateButton("Poke'em", 0, 60, 100, 40)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button
            While 1
                $Address = GUICtrlRead($Addressinput)
                $Time = GUICtrlRead($Timeinput)
                $ping = Ping($Address, $Time)
                GUICtrlSetData($log, $ping & @CRLF)
                If $ping = 0 Then
                    GUICtrlSetData($log, "No response from 1st address at " & @HOUR & ':' & @MIN & @CRLF)
                    $output = GUICtrlRead($log)
                    FileWrite("Log.txt", $output)
                    $Begin = TimerInit()
                    While 1
                        If TimerDiff($Begin) > $Time Then ExitLoop
                        If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
                    WEnd
              ; Try second server
                    $Address2 = GUICtrlRead($Addressinput2)
                    $ping2 = Ping($Address2, $Time)
                    If $ping2 = 0 Then
                        GUICtrlSetData($log, "No response from 2nd address at " & @HOUR & ':' & @MIN & @CRLF)
                        $output2 = GUICtrlRead($log)
                        FileWrite("Log.txt", $output2)
                    EndIf
                EndIf
                $Begin = TimerInit()
                While 1
                    If TimerDiff($Begin) > $Time Then ExitLoop
                    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
                WEnd
            WEnd

        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
Exit

Func _On_Exit()
    Exit
EndFunc

M23

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks

Hey how can i limit the edit box entry's coming from GUICtrlSetData($log, $ping & @CRLF,'List')

I tried

$writelog = GUICtrlSetData($log, $ping & @CRLF,'List')
GUICtrlSetLimit($writelog,10)
but that didnt work, it keeps adding line after line to the log window and in the end it would be just to much to look at and maybe crash :D

Plus i cant get out of the loop by pressing X in the top right if the programs window.

I tried creating button Case with Exit but that didnt help, its just not doing anything unless script is paused.

:D

Edited by Gettingsmarter
Link to comment
Share on other sites

  • Moderators

Gettingsmarter,

Use the Hotkey to get out of the app if the [X] does not work - I did tell you: ;-)

I also ... gave you a Hotkey to exit (Escape)

As to limiting the edit, see if this does what you want. It starts by appending to the edit as before, but then clears the edit after 10 successful pings. It also appends any unsuccessful attempts, but only writes the unsuccessful ones to the log.
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{ESC}", "_On_Exit")
FileDelete("Log.txt")
GUICreate("", 400, 200)
GUISetState(@SW_SHOW)
$log = GUICtrlCreateEdit('', 100, 0, 300, 200)
$Addressinput = GUICtrlCreateInput("Primary host", 0, 0, 100, 20)
$Addressinput2 = GUICtrlCreateInput("Backup host", 0, 20, 100, 20)
$Timeinput = GUICtrlCreateInput("15000", 0, 40, 40, 20)
GUICtrlCreateLabel("1000=1sec", 40, 43, 60)
$Button = GUICtrlCreateButton("Poke'em", 0, 60, 100, 40)

$iCount = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button
            While 1
                $Address = GUICtrlRead($Addressinput)
                $Time = GUICtrlRead($Timeinput)
                $ping = Ping($Address, $Time)
                $iCount += 1
                If $iCount < 11 Then
                    GUICtrlSetData($log, $ping & @CRLF, 1)
                Else
                    GUICtrlSetData($log, $ping & @CRLF)
                    $iCount = 0
                EndIf
                If $ping = 0 Then
                    $output = "No response from 1st address at " & @HOUR & ':' & @MIN & @CRLF
                    GUICtrlSetData($log, $output, 1)
                    FileWrite("Log.txt", $output)
                    $Begin = TimerInit()
                    While 1
                        If TimerDiff($Begin) > $Time Then ExitLoop
                        If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
                    WEnd
             ; Try second server
                    $Address2 = GUICtrlRead($Addressinput2)
                    $ping2 = Ping($Address2, $Time)
                    If $ping2 = 0 Then
                        $output2 = "No response from 2nd address at " & @HOUR & ':' & @MIN & @CRLF
                        GUICtrlSetData($log, $output2, 1)
                        FileWrite("Log.txt", $output2)
                    EndIf
                EndIf
                $Begin = TimerInit()
                While 1
                    If TimerDiff($Begin) > $Time Then ExitLoop
                    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
                WEnd
            WEnd

        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
Exit

Func _On_Exit()
    Exit
EndFunc

Just look carefully at the GUICtrlSetData statements to see how it works.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks.

I will rewrite step by step your examples and am sure i will learn allot from them. :D Only way i learn autoit is taking other ppls examples and write them one line at the time to better understand whats going on :D

Thanks a bunch !

Link to comment
Share on other sites

  • Moderators

Gettingsmarter,

Glad I could help.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...