Show internal Ip, external IP and MAC address

Post your working scripts, libraries and tools for AHK v1.1 and older
Kingron
Posts: 9
Joined: 22 May 2023, 19:47

Show internal Ip, external IP and MAC address

Post by Kingron » 23 May 2023, 07:55

Show local PC's internal IP, external IP, MAC address, location with a hot key:

Code: Select all

GetDnsAddress()
{
    if (DllCall("iphlpapi.dll\GetNetworkParams", "ptr", 0, "uint*", size) = 111) && !(VarSetCapacity(buf, size, 0))
        throw Exception("Memory allocation failed for FIXED_INFO struct", -1)
    if (DllCall("iphlpapi.dll\GetNetworkParams", "ptr", &buf, "uint*", size) != 0)
        throw Exception("GetNetworkParams failed with error: " A_LastError, -1)
    addr := &buf, DNS_SERVERS := []
    DNS_SERVERS[1] := StrGet(addr + 264 + (A_PtrSize * 2), "cp0")
    ptr := NumGet(addr+0, 264 + A_PtrSize, "uptr")
    while (ptr) {
        DNS_SERVERS[A_Index + 1] := StrGet(ptr+0 + A_PtrSize, "cp0")
        ptr := NumGet(ptr+0, "uptr")
    }
    ret := ""
    for i, v in DNS_SERVERS {
        ret := ret . "`t" . v . "`n"
    }
    return ret
}

GetMacAddress(delimiter := ":", case := False)
{
    if (DllCall("iphlpapi.dll\GetAdaptersInfo", "ptr", 0, "uint*", size) = 111) && !(VarSetCapacity(buf, size, 0))
        throw Exception("Memory allocation failed for IP_ADAPTER_INFO struct", -1)
    if (DllCall("iphlpapi.dll\GetAdaptersInfo", "ptr", &buf, "uint*", size) != 0)
        throw Exception("GetAdaptersInfo failed with error: " A_LastError, -1)
    addr := &buf, MAC_ADDRESS := []
    while (addr) {
        loop % NumGet(addr+0, 396 + A_PtrSize, "uint")
            mac .= Format("{:02" (case ? "X" : "x") "}", NumGet(addr+0, 400 + A_PtrSize + A_Index - 1, "uchar")) "" delimiter ""
        MAC_ADDRESS[A_Index] := SubStr(mac, 1, -1), mac := ""
        addr := NumGet(addr+0, "uptr")
    }
	ret := ""
	for i, v in MAC_ADDRESS {
	    ret := ret . "`t" . v . "`n"
	}
	return ret
}

GetIPs() {
	; 查询本机所有网络适配器的IP地址
	wbemLocator := ComObjCreate("WbemScripting.SWbemLocator")
	wbemServices := wbemLocator.ConnectServer(".", "root\cimv2")
	networkAdapters := wbemServices.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
	; 遍历所有网络适配器,输出IP地址
	ip := ""
	for adapter in networkAdapters
	{
		ipAddresses := adapter.IPAddress
		for index, ipAddress in ipAddresses
		{
			ip := ip . "`t" . index . "`n"
		}
	}
	whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	; whr.Open("GET", "https://api.ipify.org", false)
	extIP := ""
	try {
		whr.Open("GET", "http://myip.ipip.net/", false)
		whr.Send()
		extIP := StrReplace(whr.ResponseText, "来自于:", "`n`t")
	}
	ip := "外部地址: `n`t" . extIP . "`n" . "内部地址: `n" . ip . "`nDNS 服务器:`n" . GetDnsAddress() . "`nMAC地址:`n" . GetMacAddress()
	whr := ""
	return ip
}

showMessage(title, text) {
	Global MyBox
	Gui Msg:New, +LastFound +ToolWindow +AlwaysOnTop +Resize +MinSize200x150
	WinSet, Transparent, 240
	Gui, Msg:Font, s18
	Gui, Msg:Add, Edit, x10 y10 w680 h380 vMyBox Resize, %text%
	Gui, Msg:Show, w700 h400, %title%
	return

	MsgGuiSize:
		GuiControl, Move, MyBox, % "w" A_GuiWidth-20 " h" A_GuiHeight-20
		return
	MsgGuiEscape:
	MsgGuiClose:
	MsgGuiCancel:
		Gui,Msg:Destroy
		return
}

; Win+Shift+i 热键
#+i:: showMessage("结果(按ESC关闭)", GetIPs())

Return to “Scripts and Functions (v1)”