Function to return Default Adapter Info

Bob Sneidar bobsneidar at iotecdigital.com
Wed Oct 23 12:48:32 EDT 2019


Hi all. 

Now works with Windows! Also, I realized that it's possible to get a new DHCP lease, in which case any prior connections would be invalid, so now the function returns the default adapter IP and MAC address on the fist line, and the default router IP and MAC address on the second line. If this isn't the same between sessions, the network has changed, so reset your connection. Enjoy! 

function getDefaultNetwork pMode
   -- leave pMode empty for just IP and MAC info. Pass "Detail" to get more info
   if the platform contains "WIN" then 
      set hideConsoleWindows to true
      put "netsh interface ip show config" into tShellCommand
      put shell(tShellCommand) into tDefaultAdapter
      
      -- if pMode is detail just return these results
      if pMode is "Detail" then return tDefaultAdapter
      
      -- get the default adapter and Gateway IP addresses
      put lineOffset("IP Address:", tDefaultAdapter) into tIPAddressLine
      put word -1 of line tIPAddressLine of tDefaultAdapter into tLocalIPAddress
      put lineOffset("Default Gateway:", tDefaultAdapter)  into tGWAddressLine
      put word -1 of line tGWAddressLine of tDefaultAdapter into tGWIPAddress
      
      -- get the default adapter and gateway MAC addresses
      put "ipconfig /all" into tShellCommand
      put shell(tShellCommand) into tConfigData
      put lineOffset(tLocalIPAddress, tConfigData) -3 into tLocalMACLine
      put word -1 of line tLocalMACLine of tConfigData into tLocalMACAddress
      put "arp -a" into tShellCommand
      put shell(tShellCommand) into tArpData
      put lineOffset(tGWIPAddress, tArpData) into tGWMACAddressLine
      put word 2 of line tGWMACAddressLine of tArpData into tGWMACAddress
      
      put tLocalIPAddress && tLocalMACAddress & cr & \
            tGWIPAddress && tGWMACAddress into tCurrentAdapterInfo
   else
      -- first we need to get the default adapter
      put "route get default" into tShellCommand
      put shell(tShellCommand) into tDefaultAdapter
      put lineOffset("Interface: ", tDefaultAdapter) into tInterfaceLine
      
      if tInterfaceLine = 0 then 
         return "ERROR: No default interface found!"
      end if
      
      -- now we need the detail of the interface
      put word 2 of line tInterfaceLine of tDefaultAdapter into tDefaultInterface
      put "ipconfig getpacket " & tDefaultInterface into tShellCommand
      put shell(tShellCommand) into tInterfaceDetail
      
      -- if we didn't specifically ask for the interface and router MAC addresses, return here
      if pMode is "Detail" then return tInterfaceDetail
      
      -- now we get  the interface MAC address
      put lineOffset("chaddr", tInterfaceDetail) into tInterfaceMACLine
      put word -1 of line tInterfaceMACLine of tInterfaceDetail into tDefaultMACAddress
      
      -- and the IP address
      put lineOffset("yiaddr", tInterfaceDetail) into tInterfaceIPLine
      put word -1 of line tInterfaceIPLine of tInterfaceDetail into tDefaultIPAddress
      
      -- next we get the router IP address
      put lineOffset("router (ip_mult): ", tInterfaceDetail) into tRouterLine
      put word 3 of line tRouterLine of tInterfaceDetail into tRouterIPAddress
      put char 2 to -2 of tRouterIPAddress into tRouterIPAddress
      
      -- next we get the MAC address of the router interface
      put "arp " & tRouterIPAddress into tShellCommand
      put shell(tShellCommand) into tArpReply
      put word 4 of tArpReply into tRouterMACAddress
      
      -- finally we return the MAC addresses of the default interface and the router interface
      put tDefaultIPAddress && tDefaultMACAddress & cr & \
            tRouterIPAddress && tRouterMACAddress into tCurrentAdapterInfo
   end if
   
   return tCurrentAdapterInfo
end getDefaultNetwork



More information about the use-livecode mailing list