Jump to content

while loop I do not understand


Recommended Posts

Below is the source.

Purpose of the program is to ping all ip adresses in a given range. it works wel with ip adresses which last section is below 100. If above 100 the loop does not get executed.

If I use the same trick with do until I have no problem. Am I missing something ?

#include <GUIConstants.au3>
#include <string.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("MegaPing versie 0.2 © Copyright Carl van Dorsslaer", 543, 304, 193, 115)
$inputRange = GUICtrlCreateInput("", 24, 32, 97, 21)
$inputStart = GUICtrlCreateInput("", 144, 32, 97, 21)
$inputEnd = GUICtrlCreateInput("", 264, 32, 97, 21)
$ident = GUICtrlCreateCheckbox("ident", 365, 32, 49, 25)
$labelRange = GUICtrlCreateLabel("Range", 32, 8, 36, 17)
$labelstart = GUICtrlCreateLabel("start", 152, 8, 24, 17)
$vrij = GUICtrlCreateListView("              vrij ip", 24, 80, 120, 153)
$Progress1 = GUICtrlCreateProgress(335, 270, 175, 10)
$bezet = GUICtrlCreateListView("                 Bezet ip            |netbios naam|mogelijke verbindingen", 152, 80, 365, 153)
$Labelend = GUICtrlCreateLabel("end", 272, 8, 22, 17)
$ping = GUICtrlCreateButton("PING", 24, 272, 50, 25, 0)
$clear = GUICtrlCreateButton("CLEAR", 99, 272, 50, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Func _IpToName($ip)
    $outread = "ERROR: NbtStat.exe niet aanwezig!"

    $run = Run(@ComSpec&" /c nbname -oe "&$ip, @SystemDir, @SW_HIDE, 6)
    Sleep(500)
    $stdoutread = StdoutRead($run)
    ConsoleWrite($stdoutread)
    If Not @error = -1 Then $outread=$stdoutread
    $stderrRead = StderrRead($run)
    If Not @error = -1 Then $outread = $stderrRead
    $ss = StringSplit($outread,@CRLF)
    ConsoleWrite("----->"&$ss[0]&"<--------")
    If $ss[0] < 23 Then
        $outread=$ss[1]
        return $outread
    Else
        $i=0
        While $i < $ss[0]
        ConsoleWrite ($i&$ss[$i])
        ConsoleWrite (@CRLF)    
        $i=$i+1
        WEnd
        if $ss[23] = "" Then
            $outread = $ss[36]
        Else
            $outread = $ss[23]
        EndIf
        $outread = StringReplace($outread,Chr(129),"ü")
        $outread = StringReplace($outread,Chr(154),"Ü")
        $outread = StringReplace($outread,Chr(142),"Ä")
        $outread = StringReplace($outread,Chr(132),"ä")
        $outread = StringReplace($outread,Chr(153),"Ö")
        $outread = StringReplace($outread,Chr(148),"ö")
        $outread = StringTrimLeft($outread,4)
        $outread = StringSplit($outread," ")
        $outread = $outread[1]
        $outread = StringReplace($outread," ","",0)
    EndIf
    Return $outread
EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $clear
            GUICtrlSetData($inputRange,"")
            GUICtrlSetData($inputStart,"") 
            GUICtrlSetData($inputEnd,"")
            GUICtrlDelete($vrij)
            GUICtrlDelete($bezet)
            $vrij = GUICtrlCreateListView("              vrij ip", 24, 80, 120, 153)
            $bezet = GUICtrlCreateListView("       Bezet ip |netbios naam   |mogelijke verbindingen", 152, 80, 365, 153)
            GUICtrlSetData ($Progress1,0)
            
        Case $bezet
        Case $ping
            $start=GUICtrlRead($inputStart)
            $range=GUICtrlRead($inputRange)
            $end=GUICtrlRead($inputEnd)
            $identYes=GUICtrlRead($ident,0)
            $verschil=$end-$start
            $blokgroote=100/$verschil
            $blokstart=0
            ConsoleWrite($start&"---"&$end) ; make sure that variable $start is smaller then $end
            ; here it all gets strange
                         While $start < $end
        ;DO
                $y=$range&"."&$start
                $var = Ping($y,150)
                If $var Then
                    $x=""
                    $z=""
                    if ($identYes=1) then
                        $x= _IpToName($y)
                    EndIf
                    GUICtrlCreateListViewItem ( $y&"|"&$x&"|"&$z, $bezet)
                else
                    GUICtrlCreateListViewItem ( $y, $vrij)
                EndIf
                GUICtrlSetData ($Progress1,$blokstart)
                $blokstart=$blokstart+$blokgroote               
                $start = $start + 1
        ;Until $start > $end
            WEnd
            ConsoleWrite("finished")
    EndSwitch
WEnd
Link to comment
Share on other sites

It should word now except of the while after the Else line in the function because While...WEnd loop stops only with ExitLoop. So after entering that loop it execute those 4 lines until you exit the program. When do you want it to stop/exit that loop?

#include <GUIConstants.au3>
#include <string.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("MegaPing versie 0.2 © Copyright Carl van Dorsslaer", 543, 304, 193, 115)
$inputRange = GUICtrlCreateInput("", 24, 32, 97, 21)
$inputStart = GUICtrlCreateInput("", 144, 32, 97, 21)
$inputEnd = GUICtrlCreateInput("", 264, 32, 97, 21)
$ident = GUICtrlCreateCheckbox("ident", 365, 32, 49, 25)
$labelRange = GUICtrlCreateLabel("Range", 32, 8, 36, 17)
$labelstart = GUICtrlCreateLabel("start", 152, 8, 24, 17)
$vrij = GUICtrlCreateListView("                 vrij ip", 24, 80, 120, 153)
$Progress1 = GUICtrlCreateProgress(335, 270, 175, 10)
$bezet = GUICtrlCreateListView("                 Bezet ip             |netbios naam|mogelijke verbindingen", 152, 80, 365, 153)
$Labelend = GUICtrlCreateLabel("end", 272, 8, 22, 17)
$ping = GUICtrlCreateButton("PING", 24, 272, 50, 25, 0)
$clear = GUICtrlCreateButton("CLEAR", 99, 272, 50, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $clear
            GUICtrlSetData($inputRange,"")
            GUICtrlSetData($inputStart,"")
            GUICtrlSetData($inputEnd,"")
            GUICtrlDelete($vrij)
            GUICtrlDelete($bezet)
            $vrij = GUICtrlCreateListView("                 vrij ip", 24, 80, 120, 153)
            $bezet = GUICtrlCreateListView("       Bezet ip    |netbios naam   |mogelijke verbindingen", 152, 80, 365, 153)
            GUICtrlSetData ($Progress1,0)
            
        Case $bezet
        Case $ping
            $start=GUICtrlRead($inputStart)
            $range=GUICtrlRead($inputRange)
            $end=GUICtrlRead($inputEnd)
            $identYes=GUICtrlRead($ident,0)
            $verschil=$end-$start
            $blokgroote=100/$verschil
            $blokstart=0
            ConsoleWrite($start&"---"&$end) ; make sure that variable $start is smaller then $end
            ; here it all gets strange
        DO
                $y=$range&"."&$start
                $var = Ping($y,150)
                If $var Then
                    $x=""
                    $z=""
                    if ($identYes=1) then
                        $x= _IpToName($y)
                    EndIf
                    GUICtrlCreateListViewItem ( $y&"|"&$x&"|"&$z, $bezet)
                else
                    GUICtrlCreateListViewItem ( $y, $vrij)
                EndIf
                GUICtrlSetData ($Progress1,$blokstart)
                $blokstart=$blokstart+$blokgroote                
                $start = $start + 1
        Until $start > $end
            ConsoleWrite("finished")
    EndSwitch
WEnd

Func _IpToName($ip)
    $outread = "ERROR: NbtStat.exe niet aanwezig!"

    $run = Run(@ComSpec&" /c nbname -oe "&$ip, @SystemDir, @SW_HIDE, 6)
    Sleep(500)
    $stdoutread = StdoutRead($run)
    ConsoleWrite($stdoutread)
    If Not @error = -1 Then $outread=$stdoutread
    $stderrRead = StderrRead($run)
    If Not @error = -1 Then $outread = $stderrRead
    $ss = StringSplit($outread,@CRLF)
    ConsoleWrite("----->"&$ss[0]&"<--------")
    If $ss[0] < 23 Then
        $outread=$ss[1]
        return $outread
    Else
        $i=0
        While $i < $ss[0]
        ConsoleWrite ($i&$ss[$i])
        ConsoleWrite (@CRLF)    
        $i=$i+1
        WEnd
        if $ss[23] = "" Then
            $outread = $ss[36]
        Else
            $outread = $ss[23]
        EndIf
        $outread = StringReplace($outread,Chr(129),"ü")
        $outread = StringReplace($outread,Chr(154),"Ü")
        $outread = StringReplace($outread,Chr(142),"Ä")
        $outread = StringReplace($outread,Chr(132),"ä")
        $outread = StringReplace($outread,Chr(153),"Ö")
        $outread = StringReplace($outread,Chr(148),"ö")
        $outread = StringTrimLeft($outread,4)
        $outread = StringSplit($outread," ")
        $outread = $outread[1]
        $outread = StringReplace($outread," ","",0)
    EndIf
    Return $outread
EndFunc

EDIT: I didn't check what the script does, sorry if I messed up your script.

Edited by Aassdd
Link to comment
Share on other sites

works fine for me

Same here, source posted by thread creator works fine 100 and above...

Could it possibly be some kind of blocking being done, by firewall or maybe your ISP(if you're running it against WAN)?

Edit:

You maybe want to use the UDF _TCPIpToName instead of your _IpToName, as it requires no external programs to be run...

Edited by FreeFry
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...