Jump to content
Sign in to follow this  
computergroove

Using ping to define a global variable

Recommended Posts

computergroove

I have 5 global variables:

Global $IPAddress1 = "192.168.1.37"
Global $IPAddress2 = "192.168.1.40"
Global $IPAddress3 = "192.168.1.41"
Global $IPAddress4 = "192.168.1.47"
Global $IPAddress5 = "192.168.1.50"

Lets say I ping all the ip addresses and ip address 4 doesn't respond (it went offline). How do I make IP address 5 (192.168.1.50) be redefined as IP address 4 and have ip address 5 be nothing? Same thing for ip addresses 1-5. If I ping an ip address and it doesn't respond then I want the ip address to be removed as a global variable.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
BrewManNH

You can blank out a variable, you can't undeclare one though. If it is a local variable inside a function, the variable goes away once the function ends, if it's a global variable (any variable declared outside of a function), it is always going to exist.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
computergroove

Global $NumberOfMachines = 5

Global $IPArray[$NumberOfMachines] = [$IPAddress1, $IPAddress2, $IPAddress3, $IPAddress4, $IPAddress5]

This comes next and I have around 20 functions that use these variables. Maybe I could just write an array and remake the array every script loop where if the ping works then that ip address makes it into the array and I can change every function in my script to read from the global array instead of referencing  $IPAddress1 $IPAddress2 etc. No suggestions?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
jguinch

Why not an array ?

#Include <Array.au3>

Global $NumberOfMachines = 5
Global $aIP[$NumberOfMachines + 1] = [ $NumberOfMachines, "192.168.1.37", "192.168.1.40", "192.168.1.41", "192.168.1.47", "192.168.1.50" ]
_ArrayDisplay($aIP, "Before")

$iIndex = 1
For $i = 1 To $aIP[0]
    If Ping($aIP[$i], 250) Then
        $aIP[$iIndex] = $aIP[$i]
        $iIndex += 1
    EndIf
Next

Redim $aIP[$iIndex]
$aIP[0] = $iIndex - 1

_ArrayDisplay($aIP, "After")
Edited by jguinch

Share this post


Link to post
Share on other sites
computergroove

Global $NumberOfMachines
Global $IPAddress1 = "192.168.1.37";108"
Global $IPAddress2 = "192.168.1.40";104"
Global $IPAddress3 = "192.168.1.41";114"
Global $IPAddress4 = "192.168.1.47";117"
Global $IPArray[] = [UBound($IPArray) - 1, $IPAddress1, $IPAddress2, $IPAddress3, $IPAddress4];,"192.168.1.42"]
$NumberOfMachines = $IPArray[0]
Global $aArray[] = [Ubound($aArray) -1];Array generated by ping
For $i = 1 To $NumberOfMachines
    $Ptemp = Ping($IPArray[$i],250)
    $Count = Ubound($aArray) - 1
    If $Ptemp >0 Then
        $aArray[$Count] = $Ptemp
    EndIf
Next

The above is what I have so far - untested mostly. None of this is in my main while loop but I want the program to ping each machine every time the program loops. I have a while loop directly under this and under that I have several functions. I was under the impression that a global variable couldn't be changed from within a user defined function but I am thinking now that I am wrong on that point.

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
computergroove

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>
#include <OCR.au3>
;*****************************************************Change for new setups*****************************
Global $IPAddress1 = "192.168.1.37";108"
Global $IPAddress2 = "192.168.1.40";104"
Global $IPAddress3 = "192.168.1.41";114"
Global $IPAddress4 = "192.168.1.47";117"
Global $IPArray [] = [$IPAddress1, $IPAddress2, $IPAddress3, $IPAddress4];,"192.168.1.42"]
Global $NumberOfMachines = Ubound($IPArray)
Global $aArray[0];Array generated by ping

;*****************************************************End of Change*************************************

GetIPArray()

Func GetIPArray();make an array and eliminate dead machines
    For $i = 0 To 3
        $Ptemp = Ping($IPArray[$i],500)
        If $Ptemp >0 Then
            $Count = Ubound($aArray) - 1
            _ArrayAdd($aArray,$IPArray[$i])
        EndIf
    Next
    _ArrayDisplay($aArray)
EndFunc

I'm getting weird results in my array. With the current code I see:

[0] 192.168.1.37

[1] 192.168.1.40

and that's it. I need to see all of the ip addresses that are pinging fine. I tested the ping from a command prompt and they are all pinging with a ping faster than 250 ms. What am I doing wrong?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites
Chimp

what happens with this:

removed timeout parameter from ping command

added error report on consele  on ping failure

#include <Array.au3>
#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>
#include <OCR.au3>
;*****************************************************Change for new setups*****************************
Global $IPAddress1 = "192.168.1.37";108"
Global $IPAddress2 = "192.168.1.40";104"
Global $IPAddress3 = "192.168.1.41";114"
Global $IPAddress4 = "192.168.1.47";117"
Global $IPArray[] = [$IPAddress1, $IPAddress2, $IPAddress3, $IPAddress4];,"192.168.1.42"]
Global $NumberOfMachines = UBound($IPArray)
Global $aArray[0];Array generated by ping

;*****************************************************End of Change*************************************

GetIPArray()

Func GetIPArray();make an array and eliminate dead machines
    Local $error
    For $i = 0 To 3
        If Ping($IPArray[$i]) Then
            _ArrayAdd($aArray, $IPArray[$i])
        Else
            $error = @error
            ConsoleWrite($IPArray[$i] & " error -> " & $error & @CRLF)
        EndIf
    Next
    _ArrayDisplay($aArray)
EndFunc   ;==>GetIPArray
Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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
Sign in to follow this  

×