Sign in to follow this  
Followers 0
powaking

Find closest server

11 posts in this topic

#1 ·  Posted (edited)

Having trouble with this. Got a list of machines that I need to ping and find the closest. I'm getting an error with trying to get the smallest ping value.

Also noticed that the sorting doesn't really sort correctly. it will go from 1 to 100 then 20,30,40 etc.... Like its only sorting by the first digit from left to right.

To test just create a list called machinelist.txt with some machine names.

#include <Array.au3>
#include <file.au3>
#include <guiconstants.au3>

dim  $delay, $list, $time, $file,$var
$count = 0
$file = (@scriptdir & "\machinelist.txt")

_filereadtoarray("machinelist.txt",$list)

For $x = 1 To $list[0]
    $time=ping ($list[$x],250)
    if $time > 0 Then
    iniwrite("c:\temp\ping.ini","List",$list[$x],$time)
    EndIf
Next

$var = inireadsection("c:\temp\ping.ini","list")

;_arraydelete($var,0)
;_ArrayDisplay($var)
_arraysort($var,1,1,0,2,1)
;msgbox(16,"",$var)
;_ArrayMin($var,1,1)

;$server = _ArrayPop($var)
msgbox(16,"","Closest server is: " & $server)
filedelete("c:\temp\ping.ini")
;msgbox(16,"Quickest","Closest server is:" & @crlf & $var)
Edited by powaking

Share this post


Link to post
Share on other sites



Having trouble with this. Got a list of machines that I need to ping and find the closest. I'm getting an error with trying to get the smallest ping value.

Also noticed that the sorting doesn't really sort correctly. it will go from 1 to 100 then 20,30,40 etc.... Like its only sorting by the first digit from left to right.

To test just create a list called machinelist.txt with some machine names.

#include <Array.au3>
#include <file.au3>
#include <guiconstants.au3>

dim  $delay, $list, $time, $file,$var
$count = 0
$file = (@scriptdir & "\machinelist.txt")

_filereadtoarray("machinelist.txt",$list)

For $x = 1 To $list[0]
    $time=ping ($list[$x],250)
    if $time > 0 Then
    iniwrite("c:\temp\ping.ini","List",$list[$x],$time)
    EndIf
Next

$var = inireadsection("c:\temp\ping.ini","list")

;_arraydelete($var,0)
;_ArrayDisplay($var)
_arraysort($var,1,1,0,2,1)
;msgbox(16,"",$var)
;_ArrayMin($var,1,1)

;$server = _ArrayPop($var)
msgbox(16,"","Closest server is: " & $server)
filedelete("c:\temp\ping.ini")
;msgbox(16,"Quickest","Closest server is:" & @crlf & $var)

The sorting is alphanumeric. It sorts like a dictionary is sorted, so apple comes before but even though but is a smaller word.

To test just create a list called machinelist.txt with some machine names.

and it needs some ip addresses doesn't it?

Could you provide a small sample for us rather than ask us to make one? If we make one that's not like yours then we could be wasting our time couldn't we?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

192.168.1.0

192.168.10.0

192.168.2.0

192.168.3.0

...

192.168.9.0

Like that?

Yeah, that's going to happen. Normal and expected behavour. And rather improbable to fix, since IP addressing won't take 192.168.001.000. (Though string-sorting will...) Uhg, i just had a vision of multiple dimensional dynamic arrays...anyone got an asprin?


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

The sorting is alphanumeric. It sorts like a dictionary is sorted, so apple comes before but even though but is a smaller word.

and it needs some ip addresses doesn't it?

Could you provide a small sample for us rather than ask us to make one? If we make one that's not like yours then we could be wasting our time couldn't we?

The machinelist.txt can be either IP or machine name. Its for internal use so you can populate it with anything really. Websites even. Just want it to report back which one had the shortest ping time.

Share this post


Link to post
Share on other sites

192.168.1.0

192.168.10.0

192.168.2.0

192.168.3.0

...

192.168.9.0

Like that?

Yeah, that's going to happen. Normal and expected behavour. And rather improbable to fix, since IP addressing won't take 192.168.001.000. (Though string-sorting will...) Uhg, i just had a vision of multiple dimensional dynamic arrays...anyone got an asprin?

Actually the time results would be like this:

1

102

140

20

204

30

40

50

etc

Share this post


Link to post
Share on other sites

192.168.1.0

192.168.10.0

192.168.2.0

192.168.3.0

...

192.168.9.0

Like that?

Yeah, that's going to happen. Normal and expected behavour. And rather improbable to fix, since IP addressing won't take 192.168.001.000. (Though string-sorting will...) Uhg, i just had a vision of multiple dimensional dynamic arrays...anyone got an asprin?

Actually the time results would be like this:

1

102

140

20

204

30

40

50

etc

Actually, he was referring to sorting it numerically, not the response time.

Share this post


Link to post
Share on other sites

Actually, he was referring to sorting it numerically, not the response time.

Correct, so instead of this:

1

102

140

20

204

30

40

50

It should be:

1

20

30

40

50

102

140

204

But all I really need is to get the smallest value and display what machine has that smallest result.

Share this post


Link to post
Share on other sites

Here's a shot at it. Takes IP addresses only in machines.txt:

#include <Array.au3>
#include <file.au3>

Dim $avList, $iMinTime = 9999999, $sMinAdx = "<none>" 

; Read IP addresses from file
$sFile = (@ScriptDir & "\machinelist.txt")
If Not _FileReadToArray($sFile, $avList) Then Exit

; Remove blank lines
For $n = $avList[0] To 1 Step - 1
    If StringStripWS($avList[$n], 8) = "" Then _ArrayDelete($avList, $n)
Next
$avList[0] = UBound($avList) - 1

; Ping each address and populate results array
Dim $avResults[$avList[0] + 1][3] = [["IP Adx", "Sorting Adx", "Ping"  ]]
For $n = 1 To $avList[0]
    $avResults[$n][0] = $avList[$n]
    $avSplit = StringSplit($avList[$n], ".")
    If $avSplit[0] = 4 Then
        ; Save address as a sortable number
        $avResults[$n][1] = Number(($avSplit[1] * 1000000000) + _
                ($avSplit[2] * 1000000) + ($avSplit[3] * 1000) + $avSplit[4])
        $avResults[$n][2] = Ping($avResults[$n][0], 250)
        If $avResults[$n][2] < $iMinTime Then
            $iMinTime = $avResults[$n][2]
            $sMinAdx = $avResults[$n][0]
        EndIf
    Else
        $avResults[$n][1] = "<malformed adx>" 
    EndIf
Next

; Sort results array on reformatted IP address
_ArraySort($avResults, 0, 1, 0, 3, 2)

; Present results
MsgBox(64, "Results", "Closest server is: " & $sMinAdx & @CRLF & _
        "With ping time (in ms): " & $iMinTime)
_ArrayDisplay($avResults)

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Here's a shot at it. Takes IP addresses only in machines.txt:

#include <Array.au3>
#include <file.au3>

Dim $avList, $iMinTime = 9999999, $sMinAdx = "<none>" 

; Read IP addresses from file
$sFile = (@ScriptDir & "\machinelist.txt")
If Not _FileReadToArray($sFile, $avList) Then Exit

; Remove blank lines
For $n = $avList[0] To 1 Step - 1
    If StringStripWS($avList[$n], 8) = "" Then _ArrayDelete($avList, $n)
Next
$avList[0] = UBound($avList) - 1

; Ping each address and populate results array
Dim $avResults[$avList[0] + 1][3] = [["IP Adx", "Sorting Adx", "Ping"  ]]
For $n = 1 To $avList[0]
    $avResults[$n][0] = $avList[$n]
    $avSplit = StringSplit($avList[$n], ".")
    If $avSplit[0] = 4 Then
        ; Save address as a sortable number
        $avResults[$n][1] = Number(($avSplit[1] * 1000000000) + _
                ($avSplit[2] * 1000000) + ($avSplit[3] * 1000) + $avSplit[4])
        $avResults[$n][2] = Ping($avResults[$n][0], 250)
        If $avResults[$n][2] < $iMinTime Then
            $iMinTime = $avResults[$n][2]
            $sMinAdx = $avResults[$n][0]
        EndIf
    Else
        $avResults[$n][1] = "<malformed adx>" 
    EndIf
Next

; Sort results array on reformatted IP address
_ArraySort($avResults, 0, 1, 0, 3, 2)

; Present results
MsgBox(64, "Results", "Closest server is: " & $sMinAdx & @CRLF & _
        "With ping time (in ms): " & $iMinTime)
_ArrayDisplay($avResults)

:)

This works thanks. Just have to convert all the names to IP in the list. Hoping to have it by name but this will do.

Thanks!

Share this post


Link to post
Share on other sites

Well here is my late contribution, a slightly different take:

This basically reads all of the ip's into a 2 dimensional array, along with their ping times. Then each ip is split and each octet is padded with zeros (to make sort work properly).

The array is displayed at the end sorted by ascending ip. I included the code to sort by ping time which works but if 2 ping times are the same it does not look to the first array dimension for a comparison (limitation of _ArraySort?)

#include <Array.au3>
#include <file.au3>

Dim $resultArray[1][2]

$file = FileOpen("iplist.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$X = 0

; Read in lines of text until the EOF is reached
While 1
    $IP = FileReadLine($file)
    
    ;Exit on EOF
    If @error Then ExitLoop
    
    ;Skip empty lines
    If $IP = "" Then ContinueLoop
    
    $time = ping($IP,250)
    If $time > 0 Then
        If $X > 0 Then
            ReDim $resultArray[$X+1][2]
        EndIf

        $resultArray[$X][0] = $IP
        $resultArray[$X][1] = $time
    $X += 1    
    EndIf
Wend

;Loop through all responding IP's
For $X = 0 to Ubound($resultArray) - 1
    ;MsgBox(0,"",$resultArray[$X][0])
    ;Loop through each octet of IP and pad with zeroes
    $tempArray = StringSplit($resultArray[$X][0], ".")
    For $Y = 1 to $tempArray[0]
        $tempArray[$Y] = StringFormat("%03u",$tempArray[$Y])
    Next
    $resultArray[$X][0] = _ArrayToString($tempArray, "", 1)
Next

;Sort by ip lowest to highest
_ArraySort($resultArray, 0,0,0,2)

;Sort by ping response time lowest to highest
;_ArraySort($resultArray, 0,0,0,2,1)

_ArrayDisplay($resultArray)
FileClose($file)

Share this post


Link to post
Share on other sites

Then each ip is split and each octet is padded with zeros (to make sort work properly).

That was my first try, but I did the StringFormat() on the IP before pinging it. So if you had 10.100, 0.123 in the file, it would become 010.100.000.123. Bad news was when I tested that format on the command line to be sure ping could handle it -- it can't. A leading zero in the NT command shell indicates an OCTAL number, so it tried to ping 8.100.0.123.

Went on to try other things, but came back and tested AutoIt's Ping(), which is not bothered by that format, so I could have gone ahead with it there, it was just too late before I checked that.

Learning something new every day... :)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0