Getting the correct IP in Linux
Posted: Thu Apr 30, 2020 3:00 am
I have an issue, not really a bug, in Linux where I try to get the IP of the ethernet card to scan the LAN for servers, but the hostname tend to be associated with the loopback address.
On the Amiga and Windows the first line of code below works fine but I had to add some workarounds to get the right IP on Linux.
Using the workaround code above it can find the ethernet IP but it requires an external connection which isn't always desirable if the user has privacy/security concerns.
I'm just not sure how the network library could be improved to return the LAN IP instead of the loopback address. Maybe some NoLoopoback=True tag?
And there's also the situation where a machine has multiple network interfaces, wired and or wireless, if more than one is up, how to at least get a list to let the user pick one?
On the Amiga and Windows the first line of code below works fine but I had to add some workarounds to get the right IP on Linux.
Code: Select all
Function p_GetIP()
Local myip = ToIP(GetHostName()) /* OK on Amiga and Win, usually returns 127.0.0.1 on Linux */
/* Workaround for Linux that requires external connection */
If StartsWith(myip,"127") Or StartsWith(myip,"0.")
Local id = OpenConnection(Nil, "8.8.8.8", 53)
myip = GetLocalIP(id)
CloseConnection(id)
/* Workaround with manual user input */
If StartsWith(myip,"127") Or StartsWith(myip,"0.")
Local ip,ok = StringRequest("GetIP", "Could not find local network IP\nPlease specify your current LAN IP (IPv4)")
If ip And ok Then myip = ip
EndIf
EndIf
Return(myip)
EndFunction
I'm just not sure how the network library could be improved to return the LAN IP instead of the loopback address. Maybe some NoLoopoback=True tag?
And there's also the situation where a machine has multiple network interfaces, wired and or wireless, if more than one is up, how to at least get a list to let the user pick one?