Entry
How do I detect if the computer is online without opening a dial-up connection dialog (if offline)?
Jun 26th, 2003 13:00
James c359, Mickel Grönroos,
I'm new to Python, so I'm not fully aware of all it can do. But this
simple ping test should work. It basicaly trys to ping the computer's
gateway.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os, sre, socket, string
def OnOffLine():
# Gets your computers name and IP Address
local_host_name = socket.gethostname()
local_host_ip = socket.gethostbyname(local_host_name)
# Splits your IP and atempts to guess your gateways IP
remote_host = string.split(local_host_ip,".")
remote_host = remote_host[:-1]
remote_host_ip = ''
for item in remote_host:
remote_host_ip = remote_host_ip+item+'.'
remote_host_ip = remote_host_ip+"1"
# Pings your gateway
ping = os.popen("ping "+remote_host_ip)
data = ping.read()
ping.close()
# Searches for the number of times your hosts IP occurs and guesses
if your online or not
ping_count = sre.findall(remote_host_ip,data)
if len(ping_count) > 2:
return "Online"
else:
return "Offline"
print OnOffLine()