Home:ALL Converter>Python script to run a ping command with URL parameter

Python script to run a ping command with URL parameter

Ask Time:2018-11-29T22:44:31         Author:VGM

Json Formatter

I am trying to build a little script that helps me ping hosts by using a python script via browser.

I am stuck and I need some advice. I am new to python.

Bellow is the code I have written so far...

# !/usr/bin/python

import os.path
import urlparse

print('Content-type: text/html\r\n\r')

url = os.environ['HTTP_HOST']
uri = os.environ['REQUEST_URI']
link = url + uri

print("<br>\n{}\n<br>".format(link))

parsed = urlparse.urlparse(link)
theparameter = urlparse.parse_qs(parsed.query)['name']
thehost = (theparameter)[0]

print("===============\n{}\n===============".format(thehost))

print("<br>")
os.system("ping -c 4 {}".format(thehost))
# print("<br>")

In the apache error logs I have the following error:

malformed header from script 'test.py': Bad header: PING <theIPaddress> (<theIPaddress>)

Thank you!

Later edit: I found a solution to my question.

check = ("ping -c 4 {}".format(thehost))
result = os.popen(check).read()

print "<pre>"
print result
print "</pre>"

Author:VGM,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53541557/python-script-to-run-a-ping-command-with-url-parameter
yy