1#!/usr/bin/python
2
3"""PING cgi.
4
5Gets the name or IP number of a host as CGI argument. Returns as
6plain text the output of the ping command for that host.
7
8Lars Wirzenius <liw@wapit.com>
9"""
10
11import os, cgi, string
12
13def ping(host):
14    if len(string.split(host, "'")) != 1:
15    	return "Invalid host name."
16    f = os.popen("ping -q -c 4 '%s'" % host)
17    lines = f.readlines()
18    f.close()
19    lines = map(lambda line: line[:-1], lines)
20    lines = filter(lambda line: line and line[:4] != "--- ",  lines)
21    return string.join(string.split(string.join(lines, " ")), " ")
22
23def do_cgi():
24    print "Content-type: text/plain"
25    print ""
26
27    form = cgi.FieldStorage()
28    if not form.has_key("host"):
29	print "CGI argument `host' missing."
30    else:
31	host = form["host"].value
32	print ping(host)
33
34if __name__ == "__main__":
35    do_cgi()
36