Python小程序: ServerInformation

监听一个端口,提供HTTP接口,通过访问Web地址,调用程序获取各种信息。

例如,想在远程服务器上执行free命令查看远程服务器的内存使用情况,

free -m 的base64编码为 ZnJlZSAtbQ==,则访问下面的地址:

http://xiaoxia.org:10000/ZnJlZSAtbQ==

提交之后返回类似于下面的结果,

             total       used       free     shared    buffers     cached
Mem:           118        114          4          0          3         36
-/+ buffers/cache:         74         44
Swap:          255         56        199

为了安全使用,在CommandList文本文件里保存着一个命令列表,即可以允许调用的程序。

ps aux
free -m
netstat -na
cat /proc/meminfo
cat /proc/cpuinfo


通过访问Web默认页面:http://xiaoxia.org:10000/ 可以查看这个列表,返回如下类似结果:

Command List: 

ps aux (cHMgYXV4)
free -m (ZnJlZSAtbQ==)
netstat -na (bmV0c3RhdCAtbmE=)
cat /proc/meminfo (Y2F0IC9wcm9jL21lbWluZm8=)
cat /proc/cpuinfo (Y2F0IC9wcm9jL2NwdWluZm8=)

原始版本源代码:

  1. from BaseHTTPServer import *  
  2. import base64, os  
  3.   
  4. decorator = lambda x: x.rstrip("\n") + " (" + base64.b64encode(x.rstrip("\n")) + ")\n"  
  5.   
  6. class Handler(BaseHTTPRequestHandler):  
  7.     def process(self):  
  8.         path = self.path.split('?')[0].split('#')[0][1:]  
  9.         try:  
  10.             cmd = base64.decodestring(path)  
  11.             if cmd == "":  
  12.                 return "Command List: \n\n" + "".join(map(decorator, file("CommandList").readlines()))  
  13.         except:  
  14.             return "Try to access /[Base64 String]"  
  15.         if cmd+"\n" in file("CommandList").readlines():  
  16.             return os.popen(cmd, "r").read()  
  17.         else:  
  18.             return cmd + " is not permitted"  
  19.     def do_GET(self):  
  20.         self.send_response(200)  
  21.         buf = self.process()  
  22.         self.send_header("Content-Type""text/plain; charset=utf8")  
  23.         self.send_header("Content-Length", str(len(buf)))  
  24.         self.end_headers()  
  25.         self.wfile.write(buf)  
  26.   
  27. httpd = HTTPServer(("", 10000), Handler)  
  28. print "Server starting ..."  
  29. try:  
  30.     httpd.serve_forever()  
  31. except KeyboardInterrupt: exit()  

多线程版本源代码:

  1. from BaseHTTPServer import *  
  2. from SocketServer import ThreadingMixIn  
  3. import base64, os  
  4.   
  5. decorator = lambda x: x.rstrip("\n") + " (" + base64.b64encode(x.rstrip("\n")) + ")\n"  
  6.   
  7. class Handler(BaseHTTPRequestHandler):  
  8.     def process(self):  
  9.         path = self.path.split('?')[0].split('#')[0][1:]  
  10.         try:  
  11.             cmd = base64.decodestring(path)  
  12.             if cmd == "":  
  13.                 return "Command List: \n\n" + "".join(map(decorator, file("CommandList").readlines()))  
  14.         except:  
  15.             return "Try to access /[Base64 String]"  
  16.         if cmd+"\n" in file("CommandList").readlines():  
  17.             return os.popen(cmd, "r").read()  
  18.         else:  
  19.             return cmd + " is not permitted"  
  20.     def do_GET(self):  
  21.         self.send_response(200)  
  22.         buf = self.process()  
  23.         self.send_header("Content-Type""text/plain; charset=utf8")  
  24.         self.send_header("Content-Length", str(len(buf)))  
  25.         self.end_headers()  
  26.         self.wfile.write(buf)  
  27.           
  28. class ThreadingHTTPServer(ThreadingMixIn, HTTPServer): pass  
  29.   
  30. httpd = ThreadingHTTPServer(("", 10000), Handler)  
  31. print "Server starting ..."  
  32. try:  
  33.     httpd.serve_forever()  
  34. except KeyboardInterrupt: exit()  

Python小程序: ServerInformation》有28个想法

    1. Xiaoxia 文章作者

      如果有人喜欢执行这个命令,也可以添加到列表去。
      我没执行过,也不知道是什么结果,应该跟运行系统的时候拔出系统硬盘差不多的效果。

      回复
  1. 永恒沸点

    开始看着很危险的样子,后来看到有个文件对象啊。我说我passwd来着。哈。看了你的文章我才开始PYTHON的。谢谢你

    回复

回复 Xiaoxia 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据