Jump to content

Loop For $i


Recommended Posts

here's is a copy of the script i found on this forum and i would like to modify to fit my needs

what i want to do is have the scrip loop endless

Dim $Host = "your host here"

Dim $host_is_active = "No"

Dim $NetstatOutput

$Netstat = Run(@ComSpec & " /c netstat", '', @SW_HIDE, 2)

While 1

$NetstatData = StdoutRead($Netstat)

If @error Then ExitLoop

If $NetstatData Then

$NetstatOutput &= $NetstatData

Else

Sleep(10)

EndIf

WEnd

$NetstatOutput = StringStripWs($NetstatOutput, 6)

$NetstatOutput = StringSplit($NetstatOutput, @CRLF)

For $i = 1 To $NetstatOutput[0]

If StringInStr($NetstatOutput[$i], $Host) Then

MsgBox(1,1,"Host is active")

$host_is_active = "Yes"

EndIf

If $i = $NetstatOutput[0]-1 AND $host_is_active <> "Yes" Then

MsgBox(1,1,"Host isn't active - Rebooting")

;Shutdown(6)

EndIf

Next

Link to comment
Share on other sites

see blow

i want to continue check for host with a loop

Dim $Host = "host"

Dim $host_is_active = "No"

Dim $NetstatOutput

$Netstat = Run(@ComSpec & " /c netstat", '', @SW_HIDE, 2)

While 1

$NetstatData = StdoutRead($Netstat)

If @error Then ExitLoop

If $NetstatData Then

$NetstatOutput &= $NetstatData

Else

Sleep(30)

EndIf

WEnd

$NetstatOutput = StringStripWs($NetstatOutput, 6)

$NetstatOutput = StringSplit($NetstatOutput, @CRLF)

while 1

For $i = 1 To $NetstatOutput[0]

If StringInStr($NetstatOutput[$i], $Host) Then

ToolTip("Connected ^^", 2, 0)

$host_is_active = "Yes"

sleep(30)

EndIf

If $i = $NetstatOutput[0]-1 AND $host_is_active <> "Yes" Then

ToolTip("Restarting", 2, 0)

sleep(9000)

EndIf

Next

WEnd

Edited by PowNoob
Link to comment
Share on other sites

PowNoob,

Mod'ing your code slightly like this...

Dim $Host = "mikespc"                                                   ; Set to find my pc
Dim $host_is_active = "No"
Dim $NetstatOutput

$Netstat = Run(@ComSpec & " /c netstat", '', @SW_HIDE, 2)
While 1
    $NetstatData = StdoutRead($Netstat)
    If @error Then ExitLoop
    If $NetstatData Then
        $NetstatOutput &= $NetstatData
    Else
        Sleep(30)
    EndIf
WEnd

$NetstatOutput = StringStripWs($NetstatOutput, 6)
$NetstatOutput = StringSplit($NetstatOutput, @CRLF)

ConsoleWrite("here" & @CRLF)                                            ; At least I know got thru 1st loop

;while 1
    For $i = 1 To $NetstatOutput[0]
        If StringInStr($NetstatOutput[$i], $Host) Then
ConsoleWrite("Connected..." & @CRLF)                                    ; Show that host has been found
            ToolTip("Connected ^^", 2, 0)
            $host_is_active = "Yes"
            sleep(30)
        EndIf

        If $i = $NetstatOutput[0]-1 AND $host_is_active <> "Yes" Then
            ToolTip("Restarting", 2, 0)
            sleep(9000)
        EndIf
    Next
;WEnd

...shows that it finds my pc if I set $Host = to the name of the pc I'm working on.

It seems to me you'd be MUCH better off just pinging the host with either a known IP address or by name. That will be a lot faster.

4Eyes

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