Consider my simple case as an argument for why it's not entirely possible to do what you're asking:
- I use a cable internet connection with a DCHP-assigned dynamic IP address. ...no telling what it will be.
- My router, connected to the cable modem, has a built in firewall. Ping requests are dropped by default. Also, ports are closed unless someone on my side of the network makes a request on a specific port. By closed, I mean asleep; they won't even respond, so there's no way of telling that they're even there.
- Even if I turned on 'ping' response, the router may be turned on while no machines on my network are on. That means that you'll get a ping response, even though my network is essentially shut down.
- My router / firewall's external web configuration interface is disabled by default, so there's no way to change its settings from the outside.
So how can you ever get anything useful from that setup, unless I'm specifically broadcasting to your script or intentionally opening a port? You really can't. And the fact is, most of what I've described is the default behavior of a $50 dollar router.