Based Ping AutoIt3 the site turn IP address, as well as the hosts file written

#include <Constants.au3>
#include <GUIConstantsEx.au3>

GUICreate ( "Communications Test -eip.hxgroup.com", 400, 280)
GUICtrlCreateLabel ( "start ping 192.168.2.172", 30, 20)
$okbutton = GUICtrlCreateButton("确定", 170, 200, 80, 40)
GUISetState(@SW_SHOW)
;Sleep(2000)
Sleep(200)
GUICtrlCreateLabel(myping("192.168.2.172"), 30, 40)
Sleep(200)


GUICtrlCreateLabel ( "start detect eip.hxgroup.com whether 192.168.2.172", 30, 70)
Sleep(100)
;$run = Run(@ComSpec & ' /k ping eip.hxgroup.com', @ScriptDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;ProcessWaitClose($run)
;MsgBox(0,"",StdoutRead($run))
TCPStartup ()
If TcpNameToIp("eip.hxgroup.com")=="192.168.2.172" Then
	GUICtrlCreateLabel ( "IP address of eip.hxgroup.com [is]" & TcpNameToIp ( "eip.hxgroup.com"), 30, 90)
Else
	GUICtrlCreateLabel ( "[not] 192.168.2.172, but" & TcpNameToIp ( "eip.hxgroup.com"), 30, 90)
EndIf

Sleep(100)
GUICtrlCreateLabel ( "start writing hosts file", 30, 120)

;GUICtrlCreateLabel(FileReadLine('C:\Windows\System32\drivers\etc\hosts', -1), 30, 240)

If FileExists("C:\Windows\System32\drivers\etc\hosts") Then
; If FileExists("hosts") Then
	GUICtrlCreateLabel ( "hosts file already exists", 30, 140)
	If FileReadLine('C:\Windows\System32\drivers\etc\hosts', -1) == "192.168.2.172  eip.hxgroup.com" Then
		GUICtrlCreateLabel ( "hosts file has data", 30, 160)
	Else
		FileWriteLine('C:\Windows\System32\drivers\etc\hosts','192.168.2.172  eip.hxgroup.com')
		Sleep(200)
		GUICtrlCreateLabel ( "hosts file write completed", 30, 160)
	EndIf
Else
	GUICtrlCreateLabel ( "hosts file does not exist, start a new file and writes native hosts", 30, 140)
	$myfile = "C:\Windows\System32\drivers\etc\hosts"
	; $myfile = "hosts"
	; FileDelete ($ myfile)
	FileWriteLine($myfile,"# Copyright (c) 1993-2009 Microsoft Corp.")
	FileWriteLine($myfile, "#")
	FileWriteLine($myfile, "# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.")
	FileWriteLine($myfile, "#")
	FileWriteLine($myfile, "# This file contains the mappings of IP addresses to host names. Each")
	FileWriteLine($myfile, "# entry should be kept on an individual line. The IP address should")
	FileWriteLine($myfile, "# be placed in the first column followed by the corresponding host name.")
	FileWriteLine($myfile, "# The IP address and the host name should be separated by at least one")
	FileWriteLine($myfile, "# space.")
	FileWriteLine($myfile, "#")
	FileWriteLine($myfile, "# Additionally, comments (such as these) may be inserted on individual")
	FileWriteLine($myfile, "# lines or following the machine name denoted by a '#' symbol.")
	FileWriteLine($myfile, "#")
	FileWriteLine($myfile, "# For example:")
	FileWriteLine($myfile, "#")
	FileWriteLine($myfile, "#      102.54.94.97     rhino.acme.com          # source server")
	FileWriteLine($myfile, "#       38.25.63.10     x.acme.com              # x client host")
	FileWriteLine($myfile, "")
	FileWriteLine($myfile, "# localhost name resolution is handled within DNS itself.")
	FileWriteLine($myfile, "#	127.0.0.1       localhost")
	FileWriteLine($myfile, "#	::1             localhost")
	FileWriteLine($myfile, "192.168.2.172  eip.hxgroup.com")
	FileClose($myfile)
	GUICtrlCreateLabel ( "hosts file write completed", 30, 160)
EndIf



While 1
  $msg = GUIGetMsg()

  Select
    Case $msg = $okbutton
      ; MsgBox (0, "GUI Event", "You press the [OK] button!")
	  ExitLoop

    Case $msg = $GUI_EVENT_CLOSE
      ; MsgBox (0, "GUI Event", "! You have chosen to close Exiting ...")
      ExitLoop
  EndSelect
WEnd




Myping function ($ str)
    ; Ping website, timeout is 1000 milliseconds.
    Local $iPing = Ping($str, 1000)

    If $iPing Then
        ; MsgBox (4096, "", "Ping" & $ str & "Transceiver interval:" & $ iPing & "ms.")
		Return "Ping" & $ str & "Transceiver interval:". "Ms" & $ iPing &
    Else
        ; MsgBox (4096, "", "an error has occurred, @error is:" & @error)
		; When the function fails (returns 0) @error specific information comprising values:
		If @error == 1 Then
			Return "target host offline"
		ElseIf @error == 2 Then
			Return "destination host unreachable"
		ElseIf @error == 3 Then
			Return "wrong target"
		ElseIf @error == 4 Then
			Return "Other error - Connection timed out"
		Else
			Return "connection timed out"
		EndIf
		; = 1 target host offline
		; 2 = destination host unreachable
		; 3 = target error
		; 4 = other error
    EndIf
EndFunc   ;==>Example

  

Guess you like

Origin www.cnblogs.com/mjy2wxy/p/11445548.html