Jump to content

Need help with continual loop


ttleser
 Share

Recommended Posts

I need to write a program that'll monitor a VPN connection. I'm having a problem with the logic, cause I can't seem to get it to loop properly. It should continually ping the VPN computer until it can't, then ping the Internet. Either way it needs to report the results to the same window. I've been playing with it a while, so don't be surprised if you see something that doesn't make sense. :P

Here's the code:

#include <GuiConstants.au3>
#include <GuiListView.au3>

GUICreate("Connection Monitor", 400, 400)
GUISetIcon("SHELL32.dll", 18)
$font="Arial Black"
$font1="Arial"

; Show the GUI
$status = "VPN Connected"
GUISetState()
GUISetFont (18, 400, 0, $font)
GuiCtrlCreateLabel("Status", 25, 50,)


GUISetFont (9, 400, 0, $font1)
$button_exit = GUICtrlCreateButton('E&xit', 90, 330, 75, 23)

While 1

    $msg = GUIGetMsg()

Select
        Case $msg = $button_exit
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    Case $msg = ""
    Do

        $var = Ping("192.168.1.1", 250)
                If $var = 0 Then
        GUISetFont (18, 400, 0, $font)
        GuiCtrlCreateLabel("VPN Down", 140, 50,)
        GuiCtrlSetBkColor(-1, 0xFF0000)
                EndIF
        $var1 = Ping("www.yahoo.com", 250)
               If $var1 = 0 Then
        GUISetFont (18, 400, 0, $font)
        GuiCtrlCreateLabel("Internet Down", 140, 50,)   
        GuiCtrlSetBkColor(-1, 0xFF0000)
                EndIf

    Until $msg = $button_exit

EndSelect

WEnd
Link to comment
Share on other sites

#include <GuiConstants.au3>
   #include <GuiListView.au3>
   
   GUICreate("Connection Monitor", 400, 400)
   GUISetIcon("SHELL32.dll", 18)
   $font = "Arial Black"
   $font1 = "Arial"
   
  ; Show the GUI
   $status = "VPN Connected"
   GUISetState()
   GUISetFont(18, 400, 0, $font)
   GUICtrlCreateLabel("Status", 25, 50,)
   
   
   GUISetFont(9, 400, 0, $font1)
   $button_exit = GUICtrlCreateButton('E&xit', 90, 330, 75, 23)
   
   While 1
   
       $msg = GUIGetMsg()
   
       Select
           Case $msg = $button_exit
               Exit
           Case $msg = $GUI_EVENT_CLOSE
               Exit
       EndSelect
       $var = Ping("192.168.1.1", 250)
       If $var = 0 Then
           GUISetFont(18, 400, 0, $font)
           GUICtrlCreateLabel("VPN Down", 140, 50,)
           GUICtrlSetBkColor(-1, 0xFF0000)
       EndIf
       $var1 = Ping("www.yahoo.com", 250)
       If $var1 = 0 Then
           GUISetFont(18, 400, 0, $font)
           GUICtrlCreateLabel("Internet Down", 140, 50,)
           GUICtrlSetBkColor(-1, 0xFF0000)
       EndIf
     Sleep(10000)
   WEnd

This should work for pinging. One thing I noticed is odd though. You're pinging IP address first, but that computer will be pingable regardless of whether VPN is connected or not as long as it allows ping requests.

Live and Learn, 'cause Knowledge is Super Power.
Link to comment
Share on other sites

Copy/Pasted that code and it didn't work. Here's that didn't work.

1. GUI didn't update with current status.

2. Couldn't click the exit button, had to close it via the script icon in the tray.

To answer your questions:

You're pinging IP address first, but that computer will be pingable regardless of whether VPN is connected or not as long as it allows ping requests.

Actually 192.168.1.1 isn't on the current network, it's across the VPN connection. The current network is 192.168.3.x

If $var1 = 0 Then
           GUISetFont(18, 400, 0, $font)
           GUICtrlCreateLabel("Internet Down", 140, 50,)
           GUICtrlSetBkColor(-1, 0xFF0000)
       EndIf
     Sleep(10000)

What do you have the sleep command in here for?

Link to comment
Share on other sites

Copy/Pasted that code and it didn't work. Here's that didn't work.

1. GUI didn't update with current status.

2. Couldn't click the exit button, had to close it via the script icon in the tray.

To answer your questions:

Actually 192.168.1.1 isn't on the current network, it's across the VPN connection. The current network is 192.168.3.x

If $var1 = 0 Then
            GUISetFont(18, 400, 0, $font)
            GUICtrlCreateLabel("Internet Down", 140, 50,)
            GUICtrlSetBkColor(-1, 0xFF0000)
        EndIf
      Sleep(10000)

What do you have the sleep command in here for?

Sorry, I don't know why I put sleep in there, LOL :P Also I fixed some errors u had, mostly improper coordinates.

#include <GuiConstants.au3>
#include <GuiListView.au3>


GUICreate("Connection Monitor", 400, 400)
;GUISetIcon("SHELL32.dll", 18)
$font = "Arial Black"
$font1 = "Arial"

; Show the GUI
$status = "VPN Connected"
GUISetFont(18, 400, 0, $font)
GUICtrlCreateLabel("Status", 25, 50, 100, 60)
GUISetFont(9, 400, 0, $font1)
$button_exit = GUICtrlCreateButton('E&xit', 90, 330, 75, 23)
GUISetState()

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $button_exit
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
    $var = Ping("192.168.1.1", 32)
    If $var = 0  Then
        GUISetFont(18, 400, 0, $font)
        GUICtrlCreateLabel("VPN Down", 140, 50, 160, 40)
        GUICtrlSetBkColor(-1, 0xFF0000)
    EndIf
    $var1 = Ping("www.yahoo.com", 32)
    If $var1 = 0 Then
        GUISetFont(18, 400, 0, $font)
        GUICtrlCreateLabel("Internet Down", 140, 50, 160, 40)
        GUICtrlSetBkColor(-1, 0xFF0000)
    EndIf
WEnd
Edited by sheckandar
Live and Learn, 'cause Knowledge is Super Power.
Link to comment
Share on other sites

If you want the Gui to be responsive then remove the Sleep and use Time* functions.

example

$time = TimerInit()
While 1
    If TimerDiff($time) > 10000 Then
        $time = TimerInit()
        ;ping() every 10 seconds
        ConsoleWrite('ping' & @CRLF)
    EndIf
WEnd

You may need a visual alert while the Ping function is operating as the loop would be pause while it is pinging.

:P

Link to comment
Share on other sites

Alrighty. Thanks for the reply. I got the script working a little further and added a couple more things. Here's what I'm having problems with:

1. Window doesn't always close when I tell it to.

2. I need the time when the VPN Connect FIRST went down, not what the current time is.

3. I need the time when the Internet FIRST went down, not what the current time is.

I'd like to add a couple more time fields, like when the VPN connection and Internet Connection came back up. Any thoughts on how to do any of this?

#include <GuiConstants.au3>
#include <GuiListView.au3>
#include<date.au3>
Global $Sec, $Min, $Hour, $Time


GUICreate("Connection Monitor", 400, 400)
;GUISetIcon("SHELL32.dll", 18)
$font = "Arial Black"
$font1 = "Arial"

; Show the GUI
$vpnstatus = "Testing"
$inetstatus = "Testing"
GUISetFont(16, 400, 0, $font)
GUICtrlCreateLabel("VPN Status", 25, 50, 190, 60)
GUICtrlCreateLabel($vpnstatus, 240, 50, 90, 40)
GUICtrlSetBkColor(-1, 0x00FF00)
GUICtrlCreateLabel("Internet Status", 25, 100, 190, 60)
GUICtrlCreateLabel($inetstatus, 240, 100, 90, 40)
GUICtrlSetBkColor(-1, 0x00FF00)

;Time listings
$vpndowntime = "N/A"
$inetdowntime = "N/A"
GUISetFont(10, 400, 0, $font1)
GUICtrlCreateLabel("Last VPN Downtime", 25, 250, 190, 60)
GUICtrlCreateLabel($vpndowntime, 240, 250, 90, 40)
GUICtrlCreateLabel("Last Internet Downtime", 25, 270, 190, 60)
GUICtrlCreateLabel($inetdowntime, 240, 270, 90, 40)

GUISetFont(9, 400, 0, $font1)
$button_exit = GUICtrlCreateButton('E&xit', 90, 330, 75, 23)
GUISetState()

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $button_exit
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
    $var = Ping("192.168.1.2", 250)
    
    If $var < 1  Then
;   Msgbox (0, "test","192.168.1.2 ="&$var)
        GUISetFont(18, 400, 0, $font)
    GUICtrlCreateLabel("", 240, 50, 210, 40)
    Sleep (1000)
        GUICtrlCreateLabel("Down", 240, 50, 80, 40)
        GUICtrlSetBkColor(-1, 0xFF0000)
    Sleep (1500)
    GUISetFont(10, 400, 0, $font1)
    if @hour < "12" then
    $hour = 24 - @hour
    $ampm = "AM"
    Else
    $ampm = "PM"
    EndIF
    if @hour = 0 then
    $hour = 12 + @hour
    $ampm = "AM"
    Else
    $ampm = "PM"
    EndIF
    GUICtrlCreateLabel($hour&":"&@min&" "&$ampm, 240, 250, 90, 40)
    Else
        GUISetFont(18, 400, 0, $font)
    GUICtrlCreateLabel("Connected", 240, 50, 150, 40)
    GUICtrlSetBkColor(-1, 0x00FF00)
    EndIf
    $var1 = Ping("www.yahoo.com", 250)
    If $var1 < 1 Then
;   Msgbox (0, "test","yahoo ="&$var1)
        GUISetFont(18, 400, 0, $font)
    GUICtrlCreateLabel("", 240, 100, 210, 40)
    Sleep (1000)
        GUICtrlCreateLabel("Down", 240, 100, 80, 40)
        GUICtrlSetBkColor(-1, 0xFF0000)
    Sleep (1500)
    GUISetFont(10, 400, 0, $font1)
    if @hour < "12" then
    $hour = 24 - @hour
    $ampm = "AM"
    Else
    $ampm = "PM"
    EndIF
    if @hour = 0 then
    $hour = 12 + @hour
    $ampm = "AM"
    Else
    $ampm = "PM"
    EndIF
    GUICtrlCreateLabel($hour&":"&@min&" "&$ampm, 240, 270, 90, 40)
    Else
        GUISetFont(18, 400, 0, $font)
    GUICtrlCreateLabel("Connected", 240, 100, 150, 40)
    GUICtrlSetBkColor(-1, 0x00FF00)
    EndIf
WEnd
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...