基于AutoIt3的Ping、网址转IP地址,以及hosts文件写入

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

GUICreate("通讯测试-mjywxy.xin", 400, 280)
GUICtrlCreateLabel("开始ping xxx.xxx.xxx.xxx", 30, 20)
$okbutton = GUICtrlCreateButton("确定", 170, 200, 80, 40)
GUISetState(@SW_SHOW)
;Sleep(2000)
Sleep(200)
GUICtrlCreateLabel(myping("xxx.xxx.xxx.xxx"), 30, 40)
Sleep(200)


GUICtrlCreateLabel("开始检测mjywxy.xin是否xxx.xxx.xxx.xxx", 30, 70)
Sleep(100)
;$run = Run(@ComSpec & ' /k ping mjywxy.xin', @ScriptDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
;ProcessWaitClose($run)
;MsgBox(0,"",StdoutRead($run))
TCPStartup()
If TcpNameToIp("mjywxy.xin")=="xxx.xxx.xxx.xxx" Then
	GUICtrlCreateLabel("mjywxy.xin 的IP地址 [ 是 ] " & TcpNameToIp("mjywxy.xin"), 30, 90)
Else
	GUICtrlCreateLabel("mjywxy.xin [ 不是 ] xxx.xxx.xxx.xxx, 而是" & TcpNameToIp("mjywxy.xin"), 30, 90)
EndIf

Sleep(100)
GUICtrlCreateLabel("开始写入hosts文件", 30, 120)

;GUICtrlCreateLabel(FileReadLine('C:WindowsSystem32driversetchosts', -1), 30, 240)

If FileExists("C:WindowsSystem32driversetchosts") Then
; If FileExists("hosts") Then
	GUICtrlCreateLabel("hosts文件已存在", 30, 140)
	If FileReadLine('C:WindowsSystem32driversetchosts', -1) == "xxx.xxx.xxx.xxx  mjywxy.xin" Then
		GUICtrlCreateLabel("hosts文件已经有数据", 30, 160)
	Else
		FileWriteLine('C:WindowsSystem32driversetchosts','xxx.xxx.xxx.xxx  mjywxy.xin')
		Sleep(200)
		GUICtrlCreateLabel("写入hosts文件完成", 30, 160)
	EndIf
Else
	GUICtrlCreateLabel("hosts文件不存在,开始新建并写入原生hosts文件", 30, 140)
	$myfile = "C:WindowsSystem32driversetchosts"
	; $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, "xxx.xxx.xxx.xxx  mjywxy.xin")
	FileClose($myfile)
	GUICtrlCreateLabel("写入hosts文件完成", 30, 160)
EndIf



While 1
  $msg = GUIGetMsg()

  Select
    Case $msg = $okbutton
      ;MsgBox(0, "GUI 事件", "您按下了[确定]按钮!")
	  ExitLoop

    Case $msg = $GUI_EVENT_CLOSE
      ; MsgBox(0, "GUI 事件", "您选择了关闭!正在退出...")
      ExitLoop
  EndSelect
WEnd




Func myping($str)
    ; Ping 网站,超时时间为 1000 毫秒.
    Local $iPing = Ping($str, 1000)

    If $iPing Then
        ; MsgBox(4096, "", "Ping "& $str &" 收发时间间隔: " & $iPing & "毫秒.")
		Return "Ping "& $str &" 收发时间间隔: " & $iPing & "毫秒."
    Else
        ;MsgBox(4096, "", "发生了一个错误, @error 值为: " & @error)
		;当函数执行失败时(返回 0)@error 包含数值的具体信息:
		If @error == 1 Then
			Return "目标主机离线"
		ElseIf @error == 2 Then
			Return "目标主机无法到达"
		ElseIf @error == 3 Then
			Return "目标错误"
		ElseIf @error == 4 Then
			Return "其他错误-连接超时"
		Else
			Return "连接超时"
		EndIf
		; 1 = 目标主机离线
		; 2 = 目标主机无法到达
		; 3 = 目标错误
		; 4 = 其它错误
    EndIf
EndFunc   ;==>Example

  xxx.xxx.xxx.xxx 表示mjywxy.xin对应 ip

原文地址:https://www.cnblogs.com/mjy2wxy/p/11445548.html