How to get the public IP address of device?

Hi,

I am using an api and need to pass it the devices public ip address.

Does anyone know how to do this?

I have seen some examples using luasocket but unfortunately they only seem to cover the local ip address eg 192.168.0.101 that is connected to the router via wifi etc.

I found this code somewhere on this forum a long time ago.  Not even sure it works to be honest but I recall testing it and it seemed to.

[lua]

        require(“socket”)

        local someRandomIP = “192.168.1.122” --This address you make up

        local someRandomPort = “3102” --This port you make up  

        local mySocket = socket.udp() --Create a UDP socket like normal

        --This is the weird part, we need to set the peer for some reason

        mySocket:setpeername(someRandomIP,someRandomPort) 

        --I believe this binds the socket

        --Then we can obtain the correct ip address and port

        local myDevicesIpAddress, somePortChosenByTheOS = mySocket:getsockname()-- returns IP and Port 

[/lua]

Thanks for that,  though I just tested it and it returns my private router IP address ie 192.168.0.103 via wifi rather than my public IP address.

One option is to put a simple php script on a web server that echo’s back the IP address of any request that pings it.  You could then do a network.request() to that script, and the response will have the IP address.

  • Andrew

Right, aukStudios jogged my memory.  Here’s what ended up working for me.  Lua code to network.request the ip:

[lua]

function findClientIPAddress()

    

    local getipScriptURL = “http://xxxxxxxxxxxxxxxx/getip.php”  --php script on my server

    local DeviceIP

    

    function ipListener(event)

        if not event.isError and event.response ~= “” then

            DeviceIP = event.response 

            print(“DeviceIP:”…DeviceIP)

        end

    end

    

    network.request(getipScriptURL,“GET”,ipListener) 

    

end

findClientIPAddress()

[/lua]

And here is the entirety of the php script on my server:

<?php

   echo $_SERVER[“REMOTE_ADDR”];

?>

Thank you very much.  Worked Perfect!!!

Though I didn’t need to add my own remote server php I just called:

local getipScriptURL = "http://myip.dnsomatic.com/"

as they do exactly what you mentioned by echoing the IP address back. 

Thank you very much for the solution!

I found this code somewhere on this forum a long time ago.  Not even sure it works to be honest but I recall testing it and it seemed to.

[lua]

        require(“socket”)

        local someRandomIP = “192.168.1.122” --This address you make up

        local someRandomPort = “3102” --This port you make up  

        local mySocket = socket.udp() --Create a UDP socket like normal

        --This is the weird part, we need to set the peer for some reason

        mySocket:setpeername(someRandomIP,someRandomPort) 

        --I believe this binds the socket

        --Then we can obtain the correct ip address and port

        local myDevicesIpAddress, somePortChosenByTheOS = mySocket:getsockname()-- returns IP and Port 

[/lua]

Thanks for that,  though I just tested it and it returns my private router IP address ie 192.168.0.103 via wifi rather than my public IP address.

One option is to put a simple php script on a web server that echo’s back the IP address of any request that pings it.  You could then do a network.request() to that script, and the response will have the IP address.

  • Andrew

Right, aukStudios jogged my memory.  Here’s what ended up working for me.  Lua code to network.request the ip:

[lua]

function findClientIPAddress()

    

    local getipScriptURL = “http://xxxxxxxxxxxxxxxx/getip.php”  --php script on my server

    local DeviceIP

    

    function ipListener(event)

        if not event.isError and event.response ~= “” then

            DeviceIP = event.response 

            print(“DeviceIP:”…DeviceIP)

        end

    end

    

    network.request(getipScriptURL,“GET”,ipListener) 

    

end

findClientIPAddress()

[/lua]

And here is the entirety of the php script on my server:

<?php

   echo $_SERVER[“REMOTE_ADDR”];

?>

Thank you very much.  Worked Perfect!!!

Though I didn’t need to add my own remote server php I just called:

local getipScriptURL = "http://myip.dnsomatic.com/"

as they do exactly what you mentioned by echoing the IP address back. 

Thank you very much for the solution!