その時々

その時々で違うんです。特に決まっていないんです。

IIS+PythonCGIからPingを打ってみる

環境

Windows2003Server
IIS6.0
Python2.7

また面白いCGIが出来ました。
CGIからPingを飛ばすのです。
ただ今回ちょっとひっかかったのが、C:\Windows\System32\ping.exeを
cgiと同じディレクトリに持ってきたことです。
C:\Windows\System32にあるping.exeを直接使おうとすると
アクセス権がないのでエラーになるんですね。

py6.py

# _*_ coding: SJIS _*_

html = '''Content-Type:text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>p6</title>
</head>

<body>
  <p>Pingging Page</p>
  <p>Please Set Host or IP Address in TextBox</p>
  <form action="py6.py" method="post">
    <input type="text" name="host" />
    <input type="submit" name="submit" value="PING!" />
    <p>result</p>
    <textarea name="result" cols="80" rows="8">
      %s
    </textarea>
  </form>
</body>
</html>
'''

import cgi
import sys

f = cgi.FieldStorage()

result = ''
if f.getfirst('submit'):
    import subprocess

    host = f.getfirst('host', '')
    if len(host) == 0:
        host = 'localhost'

    ret = subprocess.Popen(['ping', host], stdout=subprocess.PIPE)
    result = ret.communicate()[0].replace('\r\n','')

print html % result

脆弱性は考慮してませんが、
なかなか簡単に出来ます。