Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Functions] Get public and local IP addresses


  • Please log in to reply
No replies to this topic
Bruttosozialprodukt
  • Members
  • 457 posts
  • Last active: Oct 18 2015 08:47 AM
  • Joined: 20 Oct 2012

Here are some functions that I use to retrieve the public and all the local IP addresses. GetPublicIP() relies on google.com which is probably the most reliable fastest solution you could get. GetLocalIPByAdaptor() returns the local IP of the network adaptor you want. You can find your adaptors names under Control Panel\Network and Internet\Network Connections. GetLocalIPs() returns an object that contains all IP addresses of adaptors that have one and the adaptors names... 
Here are some exaples and the functions, it's pretty straightforward:

MsgBox % GetPublicIP() ;Display the networks public IP
MsgBox % GetLocalIPByAdaptor("Ethernet") ;Display the local IP of the adaptor named "Ethernet"

For AdaptorName, IP in GetLocalIPs() { ;Display the IPs of all adaptors that have one
    MsgBox, %AdaptorName%: %IP%
}

GetPublicIP() {
    HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
    HttpObj.Open("GET","https://www.google.com/search?q=what+is+my+ip&num=1")
    HttpObj.Send()
    RegexMatch(HttpObj.ResponseText,"Client IP address: ([\d\.]+)",match)
    Return match1
}

GetLocalIPByAdaptor(adaptorName) {
    objWMIService := ComObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
    colItems := objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" adaptorName "'")._NewEnum, colItems[objItem]
    colItems := objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE InterfaceIndex = '" objItem.InterfaceIndex "'")._NewEnum, colItems[objItem]
    Return objItem.IPAddress[0]
}

GetLocalIPs() {
    adaptors := Object()
    ips := Object()
    objWMIService := ComObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
    colItems := objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter")._NewEnum, colItems[objItem]
    While (colItems[objItem])
        adaptors.Insert(objItem.InterfaceIndex,objItem.NetConnectionID)
    For index, name in adaptors {
        colItems := objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE InterfaceIndex = '" index "'")._NewEnum, colItems[objItem]
        If (name && objItem.IPAddress[0])
            ips.Insert(name,objItem.IPAddress[0])
    }
    Return ips
}

Credits to Blackholyman who helped me a lot with retrieving the local IPs.

 

 

...I know about A_IPAddress1/2/3/4, but it is very limited and the order differs form computer to computer.