Jump to content

Ping check


Nova
 Share

Recommended Posts

Ok the following pings a computer on my home network called comp4 and Im trying to make my script re-act according to the reply it gets !

If the message "Ping request could not find host comp4. Please check the name and try again." is read by filereadline then a msg computer dosent exist is displayed ......ect !!!

Ok heres the problem the msgbox isnt showing even when line 1 is "Ping request could not find host comp4. Please check the name and try again."

RunWait(@ComSpec & " /c " & 'ping comp4 >> C:/Temp.txt', "", @SW_HIDE)

$NoComp = ("Ping request could not find host comp4. Please check the name and try again.")

$file = FileOpen("C:/Temp.txt", 0)

If $file = -1 Then
   While 1
      $line = FileReadLine($file, 1)    
         If $line = $NoComp Then 
            MsgBox(0, "Line read:", $line)
         Else
            MsgBox(0, "Line read:", $line)
         EndIf   
   Wend

Else
   
EndIf

Why is this so ?

Edited by nova
Link to comment
Share on other sites

Here's my version.

Edit:

Fixed: You couldn't quit by clicking on Cancel

$Host = ""
If $CmdLine[0] = 0 Then
   $Host = InputBox("Ping computer", "Enter a hostname or IP", "localhost")
   If @error Then Exit
ElseIf $CmdLine[0] = 1 Then
   $Host = $CmdLine[1]
EndIf

If StringInStr($Host, "|") Then
    $Host = StringSplit($Host, "|")
    If $Host[1] == @IPAddress1 Then
        $Host = $Host[2]
    Else
        $Host = $Host[1]
    EndIf
EndIf

ProgressOn("Ping computer", "Pinging " & $Host, "", -1, -1, 16)
ProgressSet(0, "0%")
$Start = @SEC
For $i = 1 To 10
    $online = Ping($Host, 250)
    If $online Then ExitLoop
    ProgressSet($i * 10, String($i * 10) & "%")
Next
ProgressOff()

If $online Then
    MsgBox(0, "Ping computer", $Host & " is online!")
Else
    MsgBox(0, "Ping computer", $Host & " is offline!")
EndIf
Edited by SlimShady
Link to comment
Share on other sites

  • Administrators

Ok the following pings a computer on my home network called comp4 and Im trying to make my script re-act according to the reply it gets !

If the message "Ping request could not find host comp4. Please check the name and try again." is read by filereadline then a msg computer dosent exist is displayed ......ect !!!

Ok heres the problem the msgbox isnt showing even when line 1 is "Ping request could not find host comp4. Please check the name and try again."

RunWait(@ComSpec & " /c " & 'ping comp4 >> C:/Temp.txt', "", @SW_HIDE)

$NoComp = ("Ping request could not find host comp4. Please check the name and try again.")

$file = FileOpen("C:/Temp.txt", 0)

If $file = -1 Then
   While 1
      $line = FileReadLine($file, 1)    
         If $line = $NoComp Then 
            MsgBox(0, "Line read:", $line)
         Else
            MsgBox(0, "Line read:", $line)
         EndIf   
   Wend

Else
   
EndIf

Why is this so ?

I've not looked closely but I seem to remember that ping.exe creates a badly formatted textfile that AutoIt has trouble reading :)

(It's why the item on the todo list about custom file/newline terminators is there). Try using the inbuilt Ping() function in the unstable (which I think Slim has posted)

Link to comment
Share on other sites

  • Developers

heres the problem the msgbox isnt showing even when line 1 is "Ping request could not find host comp4. Please check the name and try again."

<{POST_SNAPBACK}>

Just as a general comment:

Its better to do an If StringInstr($line,"could not find host") Then in stead of comparing the whole line. The risk that you are missing spaces or something is a lot less.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Cheers to all that replyed !

Thats a real cool prog youve coded Slim I dont get all the

$CmdLine[0],$Host[2] what do the lines with [certain constant] do ?

and the double equals in this line If $Host[1] == @IPAddress1 Then

I would like to edit ur posted code to make it do what I want but I dont like doing that unless I understand it line for line !

And JdeB tnx for the idea of

If StringInstr($line,"could not find host") Then

but it still dosent work with my above posted code and I think its for the reason Jon explained

I seem to remember that ping.exe creates a badly formatted textfile that AutoIt has trouble reading

Link to comment
Share on other sites

@nova:

I commented my script.

$Host = ""
;$CmdLine is an array
;The total number of elements is stored in $CmdLine[0]
;You can use StringSplit to make a similar array
If $CmdLine[0] = 0 Then
  $Host = InputBox("Ping computer", "Enter a hostname or IP", "localhost")
  If @error Then Exit
ElseIf $CmdLine[0] = 1 Then
  ;The first command line argument is assigned to $Host
  $Host = $CmdLine[1]
EndIf

;If the first command line argument contains "|"
If StringInStr($Host, "|") Then
  ;... split the argument
   $Host = StringSplit($Host, "|")
  ;compare the first part to the info of the current computer
   If $Host[1] = @IPAddress1 OR $Host[1] = @Computername Then
       $Host = $Host[2]
   Else
       $Host = $Host[1]
   EndIf
EndIf

;Initialize the progress bar
ProgressOn("Ping computer", "Pinging " & $Host, "", -1, -1, 16)
ProgressSet(0, "0%")
;Start loop
For $i = 1 To 10
   $online = Ping($Host, 250)
   If $online Then ExitLoop
  ;Update progress bar
   ProgressSet($i * 10, String($i * 10) & "%")
Next
;Close the progress bar window
ProgressOff()

If $online Then
   MsgBox(0, "Ping computer", $Host & " is online!")
Else
   MsgBox(0, "Ping computer", $Host & " is offline!")
EndIf
Link to comment
Share on other sites

  • Administrators

I did a picture of an array for Emmanuel once, but damned if the stupid search feature will let me find it!

Edit: Might be too advanced but see http://www.autoitscript.com/forum/index.ph...035entry30035

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