Determining LAN IP

Mark Talluto userev at canelasoftware.com
Mon Aug 30 10:33:43 CDT 2010





On Aug 26, 2010, at 3:50 PM, Monte Goulding wrote:

> Hi Everyone
> 
> To determine the domain name of the computer Rev is running on we use hostName() and to turn that into an IP we use hostNameToAddress(hostName()). If you are behind a router this will return your LAN IP however if your computer is using a modem it will return a public IP. I need a reliable LAN IP so I can ask the user to enter it into their iPhone to connect to my sync server. Should I just be parsing ifconfig and ipconfig and forget about hostName() or what?
> 
> Cheers


Hi Monte,

We parse out both ifConfig and ipConfig to get the IP of a system.  We also wrote out a pairing routine to connect computers together (software remote control for our acuity systems).  It can scan an entire range on the same subnet in about two seconds on most systems.  If you would like the code send me a message.  Below is the code we use for parsing.  We made some modifications that support the subtleties of the various Win versions and wireless vs ethernet.


--> MY IP
function getMyIp
     switch (the platform)
          case "MacOS"
               put shell("/sbin/ifconfig en0") into tEthernetConfig
               put shell("/sbin/ifconfig en1") into tWirelessConfig
               
               if char 1 to 4 of tIpConfig = "zsh:" then
                    return "0.0.0.0"
               else
                    set the itemdel to "."
                    --CHECK FOR ETHERNET CONNECTION
                    get matchText(tEthernetConfig,"(?s)inet (.*?) ",retVal)  -- These are spaces on either side of (.*?)
                    if it is false then
                         --CHECK FOR WIRELESS CONNECTION
                         get matchText(tWirelessConfig,"(?s)inet (.*?) ",retVal)  -- These are spaces on either side of (.*?)
                         if it is false then
                              return "0.0.0.0"
                         end if
                    end if
               end if
               return retVal
               break
               
          case "Win32"
               put (there is not a file (specialFolderPath("system") & "/IPCONFIG.EXE")) into tWindowsError
               put (there is not a file (specialFolderPath("system") & "/SYSTEM32/IPCONFIG.EXE")) into tSys32Error
               if tWindowsError and tSys32Error then
                    return "0.0.0.0"
               else
                    set the hideConsoleWindows to true
                    put shell("ipconfig/all") into tIpConfigAll
                    
                    put tIpConfigAll into temp
                    --ETHERNET XP & VISTA
                    put offset("Ethernet adapter Local Area Connection:",temp) into tOffsetResult
                    delete char 1 to (tOffsetResult-1) in temp
                    get matchText(temp,"IP Address[\. ]*: ([A-Z0-9\.]*)",retVal)
                    
                    --WIRELESS XP & VISTA
                    if retVal is empty then
                         put tIpConfigAll into temp
                         put offset("Ethernet adapter Wireless Network Connection:",temp) into tOffsetResult
                         delete char 1 to (tOffsetResult-1) in temp
                         get matchText(temp,"IP Address[\. ]*: ([A-Z0-9\.]*)",retVal)
                    end if
                    
                    if retVal is empty then --ETHERNET WIN 7
                         put tIpConfigAll into temp
                         get matchText(temp,"IPv4 Address[\. ]*: ([A-Z0-9\.]*)",retVal)
                    end if
                    if it is false then --NO NETWORK CONNECTION FOUND
                         return "0.0.0.0"
                    else
                         return retVal
                    end if
               end if
               break
     end switch
     
     return "0.0.0.0"
end getMyIp



Best regards,

Mark Talluto
http://www.canelasoftware.com




More information about the use-livecode mailing list