Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Ping monitor - monitor ip in the background?

Thread Tools

Ping monitor - monitor ip in the background?

Posts: n/a

With python 2.6 and wxpython I'm trying to create a system tray icon
application based around an example found here:

The application will simply change the systray icon based on if an ip
address is online or not.

The ping portion looks like this:

if == "nt": # Windows
pcmd = "ping -n 1 -w 1000 "
else: # *nix
pcmd = "ping -c1 -W1 "

def Ping(ipaddress):
# execute the code and pipe the result to a string
p = subprocess.Popen(pcmd + ipaddress, shell=True,
# give it time to respond

a ='(.*)ms',
if a:
return True
return False

I've been able to add the ping check as a manual process (via a right
click menu item) but very confused on what direction to take on making
the ping a permanent loop while the app is running.

I was looking at making the ping routine a thread process but can't
figure out how to feed back the result to the calling app. Global


Reply With Quote
Andrey Balaguta
Posts: n/a
Hi, ScottZ.

I I have to write such a thing, I'll wrap the whole thing into some
class, say Pinger. It will have "do" method, which will perform one
particular pinging action. It'll also have a start/stop mechanism,
which will start a thread to continuously pinging a host. To notify
environment (say, yours tray icon) about host state change (dead/
alive), it will have callback mechanism (register_callback/
unregister_callback). Here, I've written a simple implementation,
maybe this will be helpful.

====== ================
import os
import threading
import subprocess
import re
import time

class Pinger:
def __init__(self, ip = None):
self.ip = None
self.running = False
self.callbacks = list()
def setAddress(self, ip):
if self.ip != ip:
if self.running:
self.ip = ip
def do(self):
if == "nt": # Windows
pcmd = "ping -n 1 -w 1000 "
else: # *nix
pcmd = "ping -c1 -W1 "
p = subprocess.Popen(pcmd + self.ip, shell=True,
# give it time to respond
a ='(.*)ms',
if a:
return True
return False
def start(self):
def run():
result = False
while self.running:
next =
if next != result and self.running:
[ callback(next) for callback in self.callbacks ]
result = next
self.ping_thread = threading.Thread(target = run)
self.running = True
def stop(self):
self.running = False
def register_callback(self, callback):
if callback not in self.callbacks:
def unregister_callback(self, callback):
if callback in self.callbacks:

if __name__ == '__main__':
p = Pinger('')
def printout(alive):
if alive:
print 'Host is alive.'
print 'Host is dead.'
while True:
print "Ding..."


Note that printout will be called ONLY if host state has changed, not
on EVERY ping.

Best Regards, Andrey Balaguta
Reply With Quote
Jorgen Grahn
Posts: n/a
On Sat, 01 Nov 2008 20:26:43 -0700, ScottZ <(E-Mail Removed)> wrote:
> if == "nt": # Windows
> pcmd = "ping -n 1 -w 1000 "
> else: # *nix
> pcmd = "ping -c1 -W1 "

Not really correct. Unfortunately there are many variants of ping for
Unix, and they don't take the same flags. In Solaris, for example, -c
is not a count, and -W doesn't seem to exist at all.

If I recall correctly, you can't even count on Linux installations to
have compatible pings.

Too bad that you cannot easily implement ping in your program -- it
needs extra privileges in order to handle ICMP.


// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/> R'lyeh wgah'nagl fhtagn!
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
I do ping others and viceversa but, cannot ping myself jorgeantibes Wireless Networking 0 05-15-2009 11:37 AM
VRRP : I am unable to ping the virtual address, I can only ping thebackup addresses. ATM Cisco 2 11-13-2008 09:50 PM
Can Ping Switch but Can't Ping Rtr (behind it) Bob Simon Cisco 8 01-19-2005 05:31 PM
ping ping Why gruffydd Computer Support 3 12-29-2004 05:09 PM
Can not ping myself, but can ping others =?Utf-8?B?V0pQQw==?= Wireless Networking 6 12-26-2004 05:56 AM