Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"sndping" -- ping wrapper

3 views
Skip to first unread message

Andrew Dalke

unread,
Oct 27, 1998, 3:00:00 AM10/27/98
to
Hello,

We've been having network problems and since I wanted to
know when I could get email again, I wrote "sndping", which
is a simple wrapper to the standard "ping" command. All it
really does is run "ping" (or a ping-like command) and if
the right text is in an output line, it runs a command.

Of course, the defaults are set for my machine (IRIX 6.2)
to play a sound when every successful packet it received.

I took the idea from the Jargon file entry for ping, but
since I haven't seen it implemented anywhere else, I'm
posting my copy here. It ain't much, but it's fun. You'll
probably need to tweak it for your system.

Andrew Dalke
da...@bioreason.com
#!/usr/bin/env python

# Do something (nominally, play a ping-like sound) for each
# successfully received packet from ping. Idea shameless
# taken from the Jargon File which credits Steve Hayman.
# Mike Muss at http://ftp.brl.mil/~mike/ping.html has a
# different story.
#
# Not for use in unsecure environments ('cause of os.system)
#
# I haven't tested this code except on SGIs. Don't blame me
# (or sue me) if it doesn't work or causes drought, famine, or
# baldness. Otherwise, modify and redistribute at will.

# Amusing use:
# import sndping
# sndping.ping("ls /etc", "passwd", "echo You have a passwd file")

import sys, string, os, string

## Config things here
# special options needed to make ping send repeat pings
# (Eg, on Solaris, "ping" without "-s" only tells if the remote host
# is alive). You might also want to ping every 5 seconds insted
# of 1.
ping_opt = ""

# Something to recognize in the output from ping
ping_match = "bytes from"

# The default command passed to os.system for each successful ping
# Yeah, like I know how to do sound on every OS!
def_cmd = "echo PING"

# If you want a "real" sonar ping sound, try
# http://earth.leeds.ac.uk/mgg/sounds.html
# There are many more.

# Failsafe directory if "ping" isn't in the path.
if sys.platform[:4] == "irix":
def_cmd = "/usr/sbin/sfplay /usr/demos/data/bz/pop.aiff > /dev/null"
#def_cmd = "/usr/sbin/sfplay /usr/lib/tour/sounds/action.aiff > /dev/null"
ping_dir = "/usr/etc"
elif sys.platform[:5] == "sunos":
ping_opt = "-s"
ping_dir = "/usr/sbin"
elif sys.platform[:5] == "linux":
ping_dir = "/bin"

### End of configurations

def find_ping():
global ping_dir
for dir in string.split(os.environ["PATH"], ":") + [ping_dir,]:
file = os.path.join(dir, "ping")
if os.path.exists(file):
return file
raise "Cannot find 'ping'"


def ping(ping_cmd, ping_match, cmd):
'''(ping command, output text to find, "system" command)

Run the ping(-like) command and scan the output for the given text.
If it exists, call os.system on the given command.
'''
from_ping = os.popen(ping_cmd)
try:
while 1:
line = from_ping.readline()
if line == "":
break
if string.find(line, ping_match) >= 0:
# I suppose you would rather have a callback here? :)
os.system(cmd)
except KeyboardInterrupt:
pass

def main():
global ping_opt, ping_match, def_cmd
machine = sys.argv[1]
if len(sys.argv) < 2:
print "Usage: sndping <hostname> [run command]"
sys.exit(-1)

if len(sys.argv) > 2:
cmd = sys.argv[2]
else:
cmd = def_cmd

ping_cmd = "%s %s %s" % (find_ping(), ping_opt, machine)
ping(ping_cmd, ping_match, cmd)
print "Goodbye!"

if __name__ == "__main__":
main()

0 new messages