File: ping.cgi

package info (click to toggle)
kannel 1.4.5-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,284 kB
  • sloc: ansic: 105,659; sh: 32,211; xml: 20,360; php: 1,103; perl: 711; makefile: 583; yacc: 548; awk: 133; python: 122; javascript: 27; pascal: 3
file content (35 lines) | stat: -rwxr-xr-x 839 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python3

"""PING cgi.

Gets the name or IP number of a host as CGI argument. Returns as
plain text the output of the ping command for that host.

Lars Wirzenius <liw@wapit.com>
"""

import os, cgi, string

def ping(host):
    if len(string.split(host, "'")) != 1:
    	return "Invalid host name."
    f = os.popen("ping -q -c 4 '%s'" % host)
    lines = f.readlines()
    f.close()
    lines = map(lambda line: line[:-1], lines)
    lines = filter(lambda line: line and line[:4] != "--- ",  lines)
    return string.join(string.split(string.join(lines, " ")), " ")

def do_cgi():
    print "Content-type: text/plain"
    print ""

    form = cgi.FieldStorage()
    if not form.has_key("host"):
	print "CGI argument `host' missing."
    else:
	host = form["host"].value
	print ping(host)

if __name__ == "__main__":
    do_cgi()