You cannot reliably use a single IP address, because a computer can have several network cards or several IP addresses per network card. For example, my laptop has both a wired network card and a wireless network card, and Iβm quite happy to capture two local IP addresses from my router via DHCP.
Also, in some fancy network settings (mainly corporate environments), you can have an IP address with different subnets that can talk to everyone through a router. I can have floor number 6 in the office tower 192.168.6.X, and number 7 is 192.168.7.X, and then the router allows traffic through several subnets, despite the fact that they are masked in the subnet mask.
The best strategy would be to grab a list of local IP addresses, filter loopback addresses (127.0.0.1), and then try to connect to the target machine.
But if you just want to define the local IP address as βnot publicly accessible on the Internetβ, you can try to ping the address (maybe your web server?) With a lifetime (TTL) of 1 hop. This will not pass by any router if it is present, that is, it will not work if it cannot reach the public Internet in 1 hp or less, indicating that it is behind the firewall.
Check out this great stackoverflow answer, which includes the source code: Check if the IP address is on the local network (behind firewalls and routers)
source share